When instantiating 3 clones are created rather then 1
When this code is ran i want to instantiate one SizePowerUp clone but instead 3 are created this seems to be because of the delay on the Destroy() that is there to make the bullet bounce off the brick which i need. I am not sure how to fix this problem with out removing the delay any help would be much appreciated
using UnityEngine; using System.Collections;
public class BulletCollider : MonoBehaviour {
public GameObject SizePowerUp;
void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.tag == "red")
{
Destroy(col.gameObject,.0002f);
Instantiate(SizePowerUp, transform.position, Quaternion.identity);
//ScoreManager.score += 200;
}
}
Answer by stevehealy · Feb 23, 2017 at 04:47 PM
hello i moved the instantiate on to an other script without a delay and then added a emptygame object as a child witch i added the script and a collider set to trigger to this solved my problem
Answer by UnityCoach · Feb 23, 2017 at 04:48 PM
I don't think it has to do with the Destroy, as even if you didn't destroy the object, OnTriggerEnter2D should only be called once.
If you have a hierarchy of objects and every one of them has a collider and the same tag, you may receive the message more than once.
You should try removing the Destroy instruction, and Debug.Log the name of the other object, Debug.Log (col.gameObject.name);
to make sure it really is just one object triggering this.