I’m trying to make an empty gameObject spawn projectiles, called “RedCube”. I noticed that instead of cloning the original prefab, the code keeps cloning the last spawned cube. So, when all the cubes despawn, the spawner won’t work anymore.
Here’s the code. It’s supposed to spawn the cubes on the mouse click, launch them at speed, and destroy them after a second.
#pragma strict
var RedCube : Rigidbody;
var speed = 1;
function Update () {
if (Input.GetMouseButton(0)){
RedCube = Instantiate(RedCube, transform.position, transform.rotation);
RedCube.velocity = transform.TransformDirection(Vector3(speed, 0, 0));
Destroy(RedCube.gameObject, 1);
}
}
Also, here’s a screenshot of the hierarchy. So many clones of clones of clones etc…
EDIT: Turns out the repeated cloning also makes the game run slower after about 10 seconds
First of al, you’re not instantiating a prefab but a rigidbody instead of a GameObject prefab.
And you cannot check the instantiate prefabs, they need to have a script on themselves to see if the block hit something or not or whatever.
I fixed it by creating a GameObject same as the original prefab in the hierarchy. Then I kept on instantiating that object instead of using the prefab ;).