Destroying GameObject after amount of triggers
So i tried many things final code is here:
public class destroybycontat : MonoBehaviour {
public GameObject explosion;
public GameObject playerExplosion;
public int scoreValue;
private GameController gameController;
void Start()
{
GameObject gameControllerObject = GameObject.FindWithTag("GameController");
if (gameControllerObject != null)
{
gameController = gameControllerObject.GetComponent<GameController>();
}
if (gameController == null)
{
Debug.Log("Cannot find 'GameController' script");
}
}
void OnTriggerEnter(Collider other)
{
int i = 0;
if (other.tag == "Boundary")
{
return;
}
Instantiate(explosion, transform.position, transform.rotation);
if (other.tag == "Player")
{
Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
}
if (other.gameObject.tag == "Enemy")
{
i = i + 1;
gameController.AddScore(scoreValue);
if (i == 5)
{
Destroy(gameObject);
Destroy(other.gameObject);
}
}
gameController.AddScore(scoreValue);
Destroy(other.gameObject);
Destroy(gameObject);
}
}
Look at the OnTriggerEnter method, i tagged a game object as" enemy" which includes a box collider with intrigger checked ,rigid body and a quad as a child .
As you can see im trying to count triggers to 5 and then destroy the object but lasers get him in the first shot,am im missing something...(class name missing letter i know)
Answer by babaJohnWick · Feb 11, 2017 at 11:30 PM
code gets the "Enemy" tag,i gived different value for "scoreValue" for checking.
Answer by quadplay · Feb 12, 2017 at 08:21 PM
If the problem persists, change void start () to void Awake ().
Your answer
![](https://koobas.hobune.stream/wayback/20220612110904im_/https://answers.unity.com/themes/thub/images/avi.jpg)