error CS0201
Hey guys,
I'm trying to get several prefabs to instantiate under several parent objects.
This is my code:
public GameObject [] shipSystem;
public Transform [] hardPoint;
void Start ()
{
for (int i = 0; i = 1; i++)
{
GameObject [i] installSystem = (GameObject)GameObject.Instantiate(shipSystem[i]);
installSystem [i].transform.parent = hardPoint[i].transform;
}
}
And I get this compiler error:
error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement
Does anyone know what I'm doing wrong?
Thanks.
Answer by btmedia14 · Jul 24, 2016 at 08:21 PM
The for loop expression needs a loop test condition. Instead it is showing a second assignment:
for (int i = 0; i = 1; i++)
Usually the form is something like:
for (int i = 0; i < endValue; i++)
Thank you for pointing that out. I can't believe I didn't see that. But it didn't resolve the error. It seems to be something in line 9 that it doesn't like.
Yes, I should have noticed that! The error is related to the way a new element is created and assigned to the installSystem array. Declare the installSystem array outside the for loop and then elements of the array can be assigned inside the loop.
Lets assume the end point for the loop is the number of shipSystems. Then the assignment loop would look something like:
public GameObject [] shipSystem;
public Transform [] hardPoint;
private GameObject[] installSystem;
void Start ()
{
installSystem = new GameObject[shipSystem.Length];
for (int i = 0; i < shipSystem.Length; i++)
{
installSystem[i] = (GameObject)GameObject.Instantiate(shipSystem[i]);
installSystem [i].transform.parent = hardPoint[i].transform;
}
}
Your answer