Hello, I am currently following the official space shooter tutorial and I am having this error.
NullReferenceException: Object reference not set to an instance of an object
DestroyByContact.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/DestroyByContact.cs:38)
For some reason adding the line: "gameController.AddScore(scoreValue);" ends up removing the destroy by contact atribute from the game. The explosions happens normally but neither the asteroids or the player avatar is removed from the game.
In case this line is removed, everything works but the score system.
Thank you very much for your time and attention.
public class DestroyByContact : 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 ();
}
if (gameController = null)
{
Debug.Log ("Cannot find 'Game Controller' script");
}
}
// Destroy everything that enters the trigger
void OnTriggerEnter(Collider other)
{
if (other.tag == "Boundary")
{
return;
}
Instantiate(explosion, transform.position, transform.rotation);
if (other.tag == "Player")
{
Instantiate(playerExplosion, transform.position, transform.rotation);
}
gameController.AddScore(scoreValue);
Destroy(other.gameObject);
Destroy(gameObject);
}
}
↧