- Home /
While Loop Not Assigning Array
I have this while loop in the Start function:
int i = 0;
while(i < groupCount){
patrollers[i] = Instantiate(AI, spawnPoints[i].position, spawnPoints[i].rotation) as Transform;
i++;
}
Well the AI's are being Instantiated, they just aren't assigned to the patrollers array. When I open the array, all the elements are empty. What am I doing wrong here?
But if it was an array of something else then would all be empty was my thinking. The other problem would be if AI wasn't a Transform variable - your As Transform cast would return null.
AI is a GameObject variable. This first loop has to spawn all the enemies, which is why it's in the start loop.
Answer by whydoidoit · Jul 13, 2012 at 06:52 PM
You need to get the transform from the instantiated GameObject - Instantiate returns the type of the variable which is passed in.
patrollers[i] = (Instantiate(AI, spawnPoints[i].position, spawnPoints[i].rotation) as GameObject).transform;
Your answer
Follow this Question
Related Questions
Issue instantiating an object with button click 2 Answers
Initialising List array for use in a custom Editor 1 Answer
Start() of Instantiated Prefabs Not Being Carried Out? 1 Answer
Assigning instantiated objects to a List of a Toggle button 2 Answers
Start() is called 2 times when Instantiating prefab 2 Answers