The question is answered, right answer was accepted
Running foreach command on a int array
I'm having a hard time trying understand foreach and arrays. My current code is
public void testfunction ()
{
foreach (int intValue in LoaderID)
{
Vector3 position = new Vector3(posX[intValue], posY[intValue]);
GameObject spawn = Instantiate(ObjectID[ObjChoiceID[intValue]], position, Quaternion.identity) as GameObject;
spawn.transform.localEulerAngles = new Vector3(0, 0, quant[intValue]);
spawn.transform.localScale = new Vector3(scaleX[intValue], scaleX[intValue]);
spawn.transform.parent = parentObject.transform;
}
}
What I am trying to do is use the value in LoaderID to return several values to spawn several gameobjects with specific parameters that are set in other arrays. As of right now the function spawns the objects properly but it only reads the first value in LoaderID which results in the function only spawning one game object when I want it to spawn every value in the LoaderID array.
where's the definition of LoaderID
? is it an int
array?
if so, then ins$$anonymous$$d of the foreach
, try this:
for (int intValue = 0; intValue < LoaderID.Length; intValue++)
you might want to change the position Vector3
definition to include the z
component too...
You aren't getting any errors from this? An error that occurs below the instantiate line would allow the first object to be instantiated, but would then break the rest of the foreach loop.
Have you tried putting a debug.log("Got Here"); at the end of your foreach loop to see how many times the loop completes?
The only other thing I can think of right now is that your "LoaderID" array only has one element.