Instantiation problems... :(

Hi, I am working on a script that will spawn a new ammo clip after the current one is picked up, but there is a problem. I have it set so that the clip is a child to an empty game object, and it detects when the childCount of it is less than 1. It works, but it continues to instantiate WAY too many objects (crashing my computer), and this is due to the fact that the objects arent spawned as a child to the empty object (so childCount never goes above 1). Here is my current script, and now I am trying to change the parent of the object to the object in which it was instantiated, but I get an error saying that the game object cannot be converted to a Transform :

//Item to spawn
var clip : GameObject;
//Where item will spawn and what is parented to
var spawnPoint : GameObject;
//Time to wait till spawn
var timeToSpawn : float = 5;
//If no clip on parent, spawn clip
function Update () {
	if (transform.childCount < 1) {
		SpawnClip();
	}
}

//Wait, make an item to spawn, instantiate, make child of spawn point
function SpawnClip() {
	yield WaitForSeconds (timeToSpawn);
	var ammo : GameObject;
	ammo = Instantiate (clip, transform.position, transform.rotation);
	ammo.transform.parent = (spawnPoint);
}

Can anyone tell me what to do to fix this? Thanks in advance :smile:

I believe if you use spawnPoint.transform (as opposed to spawnPoint), you’ll be fine.

ie:

ammo.transform.parent = spawnPoint.transform;

-BSpline

I think the problem is that this line:

Should look like this:

OK, did that, no console errors, but still, the object is instantiated every frame. Here is the code

//Item to spawn
var clip : GameObject;
//Where item will spawn and what is parented to
var spawnPoint : Transform;
//Time to wait till spawn
var timeToSpawn : float = 5;
//Lets it spawn in certain times
var canSpawn : boolean = false;
//If no clip on parent, spawn clip
function Update () {
		if (transform.root.childCount >= 1) {
		canSpawn = false;
		Debug.Log ("!canSpawn");
	} else if (transform.root.childCount <= 1) {
		canSpawn = true;
		Debug.Log ("canSpawn");
		SpawnClip();
	}
}

//Wait, make an item to spawn, instantiate, make child of spawn point
function SpawnClip() {
	if (canSpawn) {
	yield WaitForSeconds (timeToSpawn);
	var ammo : GameObject;
	ammo = Instantiate (clip, transform.position, transform.rotation);
	ammo.transform.parent = spawnPoint.transform;
	canSpawn = false;
	Debug.Log ("Spawned 1 clip, childCount >= 1");
	}
}

I notice in Debug.Log, it says !canSpawn before Spawned 1 clip, childCount >= 1, and that doesnt make sense because canSpawn has to be true in order for it to instantiate, so IDK whats going on here, maybe you could help? :?

maybe it is because childcount i updated in FixedUpdate and therefore runs through your script many times before childcount is updated.

Try to move your script to FixedUpdate instead.

Thats not it… and isnt FixedUpdate for physics?

ahh sorry. I didn’t noticed that you had the

yield WaitForSeconds (timeToSpawn);

and because of that do your script execute the

function SpawnClip many times until the first Instantiate is executed five seconds later.

hope it make sence