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*.transform.parent = transform;*

spawnedObjects*.transform.position = transform.GetChild(i).position;
_spawnedObjects.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.

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.

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.

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 :stuck_out_tongue:

Thank you guys for your time- I hope this will be helpful for someone doing the same mistake as me down the road lol. :slight_smile: