cloning crashing unity

I have a gameobject, when it collides with another gameobject it destroys itself and a clone takes it place of where it was destroyed. However, when it clones itself it causes Unity to crash. Any idea why this is?

	void OnCollisionEnter(Collision hit)
	{
		if(hit.gameObject.tag == "Wall")
		{
			Destroy(gameObject);
			GameObject clone = Instantiate(boltprefab, transform.position, transform.rotation) as GameObject;
			clone.gameObject.tag = null; //prevents multiple clones
		}
	}

As a guess change line 7 to: clone.gameObject.tag = ""; I'm guessing that there is an attempt to access the tag somewhere else, and you are generating a null reference exception.

alright it doesn't crash anymore, but the changed caused it to become boltprefab(clone)(clone)(clone).....(clone) in the hierarchy , have a feeling it that's a bad thing, since it should be just "boltprefab(clone)" and will crash the more of those I have in the scene

1 Answer

1

void OnCollisionEnter(Collision hit)
{
if(hit.gameObject.tag == “Wall”)
{
GameObject clone = Instantiate(boltprefab, transform.position, transform.rotation) as GameObject;
clone.gameObject.tag = “Untagged”; //you must assign some tag
Destroy(gameObject);
}
}

how would I remove the clones rigidbody and collider? Destroy(clone.gameObject.collider); Destroy(clone.gameObject.rigidbody); //or Destroy(clone.collider); Destroy(clone.rigidbody); doesn't seem to work

clone is also a gameObject, Destroy(clone.collider); Destroy(clone.rigidbody);

I've tried both methods, but for some reason neither is removing the collider or rigidbody

post ur code and try this:Destroy(clone.GetComponent(collider));