-2

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.

7
  • @GrantWinney , It says GameController is null , but why? Commented Mar 19, 2016 at 15:32
  • @Lucas Trzesniewski , I think my question is not very similar to the master duplicate question you said , sure it's same problem , but with reopen it , maybe someobe will help me , Thanks! Commented Mar 19, 2016 at 20:27
  • @rene thanks for pointing me to this, I've responded on meta. Commented Mar 20, 2016 at 2:07
  • 1
    Closed. It is an exact duplicate - with you having never learned even the most basic debugging. Identifying what is null is trivial. If you say it is not identical then with all respect, spend half an hour learning how to use a debugger and doing some basic standard steps yourself. Commented Mar 20, 2016 at 4:43
  • @TomTom , I see alot of NRE question in SO, but many of them are not closed as duplicate , should all of them be closed? or jusr mine is same to master question? Commented Mar 20, 2016 at 8:09

1 Answer 1

3

In you current code, the line :

gameController = gameControllerObject.GetComponent<GameController>();

will never execute, since you are checking if gameController is not null before actually assigning it.

I think your mistake is in the first if (gameController != null). You should check if gameControllerObject is not null instead, like so :

GameObject gameControllerObject = GameObject.FindGameObjectWithTag("GameController");
    if (gameControllerObject != null) //Replace gameController with gameControllerObject 
    {
        gameController = gameControllerObject.GetComponent<GameController>();
    }
    if (gameController == null)
    {
        Debug.Log("Cannot find 'GameController' script!");
    }
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.