IndexOutOfRangeException: Index was outside the bounds of the array. Spawn.Update () (at Assets/Scripts/Spawn.cs:38). как решить ошибку?

Вылезла ошибка IndexOutOfRangeException: Index was outside the bounds of the array. Spawn.Update () (at Assets/Scripts/Spawn.cs:38) именно в самом юнити, а не в Visual Studio. Как решить?

Вот код:

public class Spawn : MonoBehaviour
{
    public GameObject [] Spawner;

    public Transform[] spawnTree;
    private int randX;
    private int randY;

    private int rand;
    private int randPosition;
    private float spawnRate = 120f;
    float nextSpawn;
    
    void Start()
    {
        
    }

    void Update()
    {
        if (Time.time > nextSpawn)           
        {
            nextSpawn = Time.time + spawnRate;
            rand = Random.Range(0, Spawner.Length);
            randPosition = Random.Range(0, spawnTree.Length);

            Instantiate(Spawner[rand], spawnTree[randPosition].transform.position, Quaternion.identity);
        }
    }
}

Ответы (1 шт):

Автор решения: MyNameIsKitsune

Вместо Spawner.Length поставь Spawner.Length-1. Потому что С# любит считать с 1

→ Ссылка