N
N
NitroFuNyx2020-06-24 16:25:19
Android
NitroFuNyx, 2020-06-24 16:25:19

Unity error IndexOutOfRangeException: Array index is out of range?

public GameObject[] obj;
    private GameObject installed_object;
    private int rand=0;
    public int spawnTime=50;
    private float TimeToDeploy=1;
    // Start is called before the first frame update
    void Start()
    {
        
        
    }

    // Update is called once per frame
    void Update()
    {
        if (TimeToDeploy >= 150)
        {
            rand = Random.Range(0, obj.Length - 1);
            installed_object = Instantiate(obj[rand], obj[rand].transform.position, Quaternion.identity)  as GameObject;
            installed_object.transform.localPosition = new Vector3(0, 3.2f, 36);
            TimeToDeploy -= 150;
        }
        else
            TimeToDeploy += Time.deltaTime*spawnTime;
    }


The error is here:
installed_object = Instantiate(obj[rand], obj[rand].transform.position, Quaternion.identity)  as GameObject;

There are no ideas at all. Tried to do something myself. As a result - nothing)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
NitroFuNyx, 2020-06-24
@NitroFuNyx

Rave. As it always happens ... I recreated the script already on the cube and not on the GameObject and everything worked. Returned as it was and there are no errors. Works as with (0, obj.Length); So is (0, obj.Length-1);
I don't understand these jokes.

T
Timur Pokrovsky, 2020-06-24
@Makaroshka007

Try this:
rand = Random.Range(0, obj.Length);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question