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.