-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.

Community
  • 1
  • 1
ᴀʀᴍᴀɴ
  • 4,443
  • 8
  • 37
  • 57
  • @GrantWinney , It says `GameController` is null , but why? – ᴀʀᴍᴀɴ Mar 19 '16 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! – ᴀʀᴍᴀɴ Mar 19 '16 at 20:27
  • @rene thanks for pointing me to this, I've responded on meta. – Lucas Trzesniewski Mar 20 '16 at 02: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. – TomTom Mar 20 '16 at 04: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? – ᴀʀᴍᴀɴ Mar 20 '16 at 08:09
  • Pretty much all of them. There pretty much never is a valid NRE question - finding the reason for a NRE (which variable is null) is trivial. That is like cooking boiling water. Now, if you ask why GetComponent returns null with the code how you configure it etc. - THAT would be a different question and may be valid. – TomTom Mar 20 '16 at 08:15
  • @TomTom , no error was from`if` condition ,I'm new to c# and I didn't knew that NRE is a common error , however thank you! – ᴀʀᴍᴀɴ Mar 20 '16 at 08:17

1 Answers1

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!");
    }
JoRouss
  • 2,864
  • 2
  • 22
  • 40