【问题标题】:Unity3d scripting error c# IndexOutOfRangeException: Array index is out of rangeUnity3d脚本错误c#IndexOutOfRangeException:数组索引超出范围
【发布时间】:2017-02-28 00:08:34
【问题描述】:

这是我用c#写的下面的生成脚本脚本应该在场景中随机创建对象。

问题是我在运行时收到此错误。

IndexOutOfRangeException: Array index is out of range.
CreateEasterEggs.MakeThingToSpawn () (at Assets/CreateEasterEggs.cs:52)
CreateEasterEggs.Update () (at Assets/CreateEasterEggs.cs:28)

不确定我做错了什么,认为它与游戏对象有关?

谢谢。


using UnityEngine;
 using System.Collections;

 public class CreateEasterEggs : MonoBehaviour
 {
     public float secondsBetweenSpawning = 0.1f;
     public float xMinRange = -25.0f;
     public float xMaxRange = 25.0f;
     public float yMinRange = -5.0f;
     public float yMaxRange = 0.0f;
     public float zMinRange = -25.0f;
     public float zMaxRange = 25.0f;
     public GameObject[] spawnObjects; // what prefabs to spawn

     private float nextSpawnTime;

     void Start ()
     {
         // determine when to spawn the next object
         nextSpawnTime = Time.time+secondsBetweenSpawning;
     }

     void Update ()
     {
         // if time to spawn a new game object
         if (Time.time  >= nextSpawnTime) {
             // Spawn the game object through function below
             MakeThingToSpawn ();

             // determine the next time to spawn the object
             nextSpawnTime = Time.time+secondsBetweenSpawning;
         }   
     }

    void MakeThingToSpawn ()
      {
          //Start the vector at an invalid position
          Vector3 spawnPosition = new Vector3(0, 0, 0);

          //while we are not in the right range, continually regenerate the position
          while ((spawnPosition.z < 4 && spawnPosition.z > -4) || (spawnPosition.x < 4 && spawnPosition.x > -4)) 
          {
              spawnPosition.x = Random.Range (xMinRange, xMaxRange);
              spawnPosition.y = Random.Range (yMinRange, yMaxRange);
              spawnPosition.z = Random.Range (zMinRange, zMaxRange);
          }

          // determine which object to spawn
          int objectToSpawn = Random.Range (0, spawnObjects.Length);

          // actually spawn the game object
              GameObject spawnedObject = Instantiate (spawnObjects [objectToSpawn], spawnPosition, transform.rotation) as GameObject;

          // make the parent the spawner so hierarchy doesn't get super messy
          spawnedObject.transform.parent = gameObject.transform;
      }
 }

【问题讨论】:

  • 我唯一能看到的可能是 spawnObjects 数组为空。您是否已将 GameObjects 添加到检查器中的数组中?否则,您可以打印 objectToSpawn 并查看打印的内容并从那里开始。
  • @JohanLindkvist ;-/ 是的,就是这样,谢谢!让它成为答案,我会接受。

标签: c# unity3d unity5


【解决方案1】:

IndexOutOfRange 表示您试图访问一个不存在的数组元素。

在您使用Random.Range (0, spawnObjects.Length); 的情况下,唯一可能的情况是您的数组为空。

尝试在Instantiate 之前输入Debug.Log(spawnObjects.Length):,您会看到实际上您的游戏对象数组是空的,因为它将返回0。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多