I’m having some trouble getting a counter to work in a space shooter game. The idea is that you need to shoot an asteroid 3 times in order for it to explode. I’m using colliders and if statements to do this but nothing in the actual check for counter works but the if statement checking the collision that it is nested in works fine.
Code
using UnityEngine;
using System.Collections;
public class Mover : MonoBehaviour {
public float speed;
public GameObject explosion;
double counter = 0;
void Start () {
GetComponent<Rigidbody> ().velocity = transform.forward * speed;
}
void OnTriggerEnter(Collider other){
if (other.gameObject.tag == "Boundary") {
GameObject.Destroy(gameObject);
}
if (other.gameObject.tag == "Asteroid") {
counter = counter +1;
Instantiate (explosion, transform.position, transform.rotation);
Debug.Log(counter);
if(counter == 3){
Instantiate (explosion, transform.position, transform.rotation);
GameObject.Destroy(other.gameObject);
GameObject.Destroy(gameObject);
}
Debug.Log(counter);
}
}
}
2272655–152377–Mover.cs (746 Bytes)