- Home /
For Loop won't stop initiating
void Spawner()
{
float shift = 0.5f;
for (int curCount = 0; curCount <= 4; curCount++)
{
GameObject Snaker = Instantiate(snakerHead,
new Vector3(snakerHead.transform.position.x,
snakerHead.transform.position.y,
snakerHead.transform.position.z + shift),
Quaternion.identity) as GameObject;
shift += 0.5f;
Snaker.transform.parent = snakerHead.transform;
eSnake.Add(Snaker);
}
}
void Awake()
{
Spawner ();
}
I've tried using the break; command, but that doesn't make it stop either. I've also tried to make it so that it only calls it if curCount != 4.
Oh top of that, it starts parenting to the child over the course of the spawning.
You wouldn't happen to have the Spawner script attached to the snakerHead prefab by chance would you?
Answer by RaulG · Jan 13, 2015 at 08:21 AM
I think I figured it out. For some reason the line
Snaker.transform.parent = snakerHead.transform;
Was causing some issues.
First it would create the first 4 objects, and then randomly parent child objects to the child objects infinitely.
Dunno why it would do that, but I moved it over to the Update and now it works!
I think there are other problems with the design. snakerHead
is the thing you're spawning. $$anonymous$$ost people would have it as a prefab, and making a real gameObject be the child of a not-in-the-world prefab makes no sense.
If snakerHead is a real game object, then Instantiate will copy it, sure, but that almost always causes odd problems later.
Either way, you copy it then add it to itself, making it double in size. Is that what you really wanted?