Question by
SebastianEsp · Jan 03, 2016 at 05:54 PM ·
unity 5arrayinspector
Array size resets to 0 in inspector.
I have a script that is supposed to spawn a number of GameObjects from an array. The problem occours when i try to set the array size in the inspector. Instead of the size being set, it imidiatly resets to 0.
public class GamePiece : MonoBehaviour
{
public GameObject[] Blocks;
public Transform pos;
public int blockCount;
private float waitTime = 0.5f;
private GameObject block;
// Use this for initialization
void Start ()
{
Spawn();
}
// Update is called once per frame
void Update ()
{
}
public void Spawn()
{
block = Instantiate(Blocks [Random.Range(0,5)], pos.position, Quaternion.identity) as GameObject;
Vector3 tmp = pos.position;
tmp.y = 0;
tmp.x += 5.11f;
tmp.z = 0;
pos.position = tmp;
StartCoroutine(wait());
}
IEnumerator wait()
{
yield return new WaitForSeconds(waitTime);
blockCount += 1;
Spawn();
}
Comment
I don't see why it does that, the code doesn't show any problems. Although (Blocks [Random.Range(0,5)]
will work. It is safer to do (Blocks [Random.Range(0,Blocks.Length)]
to avoid any null exceptions in the future. But try removing the script from the object and putting the script on the gameobject again. Also Vector3 tmp = pos.position + Vector3(5.11f, 0f, 0f);
is cleaner.
Are you sure you have assigned objects to the array in the inspector?