I'm trying to make a game using unity 5 but I face a problem in this level here is my GameController.cs
:
public class GameController : MonoBehaviour
{
private int score;
void Start()
{
score = 0;
UpdateScore();
}
public void AddScore(int newScore)
{
score += newScore;
UpdateScore();
}
void UpdateScore()
{
scoreText.text = "Score : " + score.ToString();
}
This not the full code , this the only related part of the code , and this DestroyByContact.cs
:
public class DestroyByContact : MonoBehaviour
{
private GameController gameController;
public int scoreValue;
void Start()
{
GameObject gameControllerObject = GameObject.FindGameObjectWithTag("GameController");
if (gameController != null)
{
gameController = gameControllerObject.GetComponent<GameController>();
}
if (gameController == null)
{
Debug.Log("Cannot find 'GameController' script!");
}
}
void OnTriggerEnter(Collider other)
{
Debug.Log(scoreValue);
gameController.AddScore(scoreValue); # This is line 38
Destroy(other.gameObject);
Destroy(this.gameObject);
}
}
And this the full Error I get from Unity
console:
NullReferenceException: Object reference not set to an instance of an object
DestroyByContact.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Assets/Scripts/DestroyByContact.cs:38)
I assgin all references in unity correct ,Score
remain at 0
and Object will not destroy however before adding this they would destory, Can please help me correct this error?
Duplicate Notice
I read the accepted answer to master duplicate question but It's a very general one(It lists all type of this error and what is going to make them but I realy dont know which make this error for me),and just because I add all related code I think it's a very common mistake and other future users with get benefits from this answer, maybe with reopen the question , someone will help me correct the error.