Find gameobject that has increasing number

Viewed 45

I have few game objects to find that have the names format as "transform_1", "transform_2", "transform_3", etc. so the number always increases. So how do I find the game object when i++ and my gameObject ends with i? Currently, Positions[0] = empty and Positions[1] = transform_1. This is not what I want. How do I make it so that Positions[0] = transform_1 and so on?

public GameObject[] Positions;

void Start()
    {
         for(int i=0;i<Positions.Length;i++)
        {
            Positions[i] = GameObject.Find("transform_"+i);
           
        }
    }
1 Answers

Change Positions.Length to Positions.Length-1

This is because the script already starts from zero and looping to Positions.Length would cause the array to go to long.

The 2nd change is to change "transform_"+i to "transorm_"+(i-1)"

This makes it so that the correct game object is selected and cause Positions[0] to equal transform_1

Related