- Home /
Instantiate script crashes unity
So I'm making a simple spawn system for my horror game, which is going to spawn things like notes and batteries at random on the game start at certain predefined spawnpoints, but I have a problem with the script crashing. The script works fine if I only instantiate one object- Here's the script:
public class SpawnSystem : MonoBehaviour
{
public GameObject ObjectToSpawn;
private List<GameObject> _spawnedObjects;
// Use this for initialization
void Start ()
{
_spawnedObjects = new List<GameObject>();
for(int i = 0; i < transform.childCount; i++)
{
_spawnedObjects.Add((GameObject)Instantiate(ObjectToSpawn));
_spawnedObjects[i].transform.parent = transform;
_spawnedObjects[i].transform.position = transform.GetChild(i).position;
_spawnedObjects[i].transform.rotation = transform.GetChild(i).rotation;
}
}
}
At first I thought it crashed because I simply had a local variable inside the for loop holding the instantiated value, but making a list of gameobjects to contain them didn't help. The spawn points are simply empty gameobjects which are children to the object this script is attached to. The observed behavior is that I start my game, sounds starts playing, as it usually does while the game is loading, but after a few seconds the sound stops, and everything freezes. Am I not allowed to instantiate this many objects in one cycle? (currently I'm only testing with 4 objects)
Any help would be appreciated.
Thanks for reading.
What do you mean by "crashing"? be more specific.
Does the game just stop playing and return to the editor?
Does unity actually crash and die such that you have to restart unity?
When I say crash, I mean everything freezes as to the point where I have to forcibly close unity through the task manager in order to get moving- It's totally frozen, as if in an infinite loop, or is loading something so massively that it just freezes.
I am currently testing with "ObjectToSpawn" being a battery object- It's just a prefab with a model, a collider, and a script. I've just tried removing the script, with no change in behavior.
I currently only have 1 object with this script attached.
I am not sure what else information there is to give you that seems relevant?
There is no "hocus pocus" about the object being spawned, or the object spawning the objects. Basically it's like this:
There's a parent object with the above script attached. It has 4 child objects, that are pure empty gameobjects. Currently there's not even anything random about this, it just iterates through the children in the code, and is supposed to spawn a battery at each location. The children holds all the information I need from that in the transform (Position, and rotation).
I am not sure what else relevant information I have to give? As I stated earlier this works when I just instantiate 1 object (No for loop).
Thanks for your time :-)
Answer by Bunnybomb7670 · Jun 23, 2014 at 10:28 PM
I think it goes into an infinite loop by doing the for loop based on the childCount. You basically loop for every child, then in your loop add another child and have to loop again, and so on. You need to store the size before the loop I think to prevent it from infinitely looping through children. Setting the Transform.parent is the issue because it makes it into a child, therefore increasing the child count.
Simply store an integer of the childcount before the loop, then use that as the length of the loop, rather than accessing the childcount value.
Changed the "accepted answer" to yours-. Since my answers are being moderated , they take a while to appear, so I think I posted my own answer earlier than posted above- But nevertheless you spend time on helping me, so you deserve the "accepted answer" :-) Thanks a lot.
Answer by Bunnybomb7670 · Jun 23, 2014 at 09:31 PM
I cannot tell much from what you have posted, but by any chance is the variable : ObjectToSpawn have a script : SpawnSystem on it? If so, that script will basically spawn however many objects it should, then if they have spawnsystems on them, they will all spawn however many spawnsystems they have been told to and it will continue like that in an infinite loop. That is the only reason I can see why it is crashing unless you give us more information.
Answer by MKayJay · Jun 23, 2014 at 10:29 PM
So I just figured it out myself.
The problem is I am taking the childcount information directly from the transform, which means, that everytime I spawn an object, the child count goes up- thus we end up with an infinite loop. So the solution is easy, just cache the initial childcount in a variable before going into the for loop... phew., right infront of my eyes :P
Thank you guys for your time- I hope this will be helpful for someone doing the same mistake as me down the road lol. :-)
I literally wrote that solution 1 $$anonymous$$ute before you posted :D
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
What's best: Instantiate or LoadLevelAdditive? 2 Answers
2.5d game instantiate problem 1 Answer
loading elements on startup 2 Answers
GUI Button and for loop not working 2 Answers