-2

I know, this question was already answered a lot here, and believe me, I've tried so many ways to fix that problem, but it occurs over and over again.

So basically, I'm trying to change e.g. money in my game, from a different script.

But as soon as I click the button, I get this error message. I think I'm doing anything fundamentally wrong here, but it also happens in my score script, but that is still working anyhow... But here is the error:

NullReferenceException: Object reference not set to an instance of an object
Score.ResetScore () (at Assets/Scripts/Score.cs:36)

And here are the Scripts that should work together.

Script 1:

void ResetScore()
    {
        GameManager gamemanag = GetComponent<GameManager>();
        score = 0;
        gamemanag.ResetQuestions();
    }

Script 2:

public void ResetQuestions()
    {
        unansweredQuestions = questions.ToList<Question>();
    }

That was the score script because it's a bit cleaner. This doesn't really work as it should as well and I have no Idea why...

I'm posting the full code on pastebin at the end.

Would be great if you could help!

Script 1: http://pastebin.com/raw/qvbFYd3x

Script 2: http://pastebin.com/raw/8gMzaagq

thaiten
  • 47
  • 8
  • 2
    The only way that that `ResetScore()` can throw a `NullReferenceException` is if `GetComponent()` returns `null`. Which is possible depending on configuration. Which makes me think that the problem isn't in your code, but in your configuration, which isn't part of your question. –  Dec 18 '16 at 17:57

2 Answers2

1

There are multiple things going on:

  1. (Why are you setting score to 0 if score == 0? You could just leave the else away.)

  2. In DisplayScore you are accessing the scoreText field. That will throw a NullReferenceException if you haven't set it in the inspector.

  3. In ResetScore you are getting the GameManager instance. The call to ResetQuestions will throw a NullReferenceException if your gameobject that contains the Score script doesn't contain the GameManager script too.

  4. In Start of the GameManager you could get a NullReferenceException if you haven't set any question in the inspector.

  5. You have an off by one error in your SetCurrentQuestion. Basically if there are no questions you are getting a random number from 0 to 0. The only valid result is 0 in that situation. You are then accessing question with index 0. But there won't be one. (That would throw an IndexOutOfRangeException)

  6. Any of your private [SerializeField] fields could throw a NullReferenceException if it hasn't been assigned in the inspector.

As you can see, many of your statements could result in a NullReferenceException. We won't be able to actually tell you where the issue is, because it could be in so many places. You are best off by debugging your code and checking where a variable you are trying to access is null.

Noel Widmer
  • 4,444
  • 9
  • 45
  • 69
  • Thanks for your answer! I've changed it in ResetScore to if (score != 0) score = 0; So Number 1 is out I guess. I still get the error. I've set the Scoretext everywhere, so number 2 is out as well I guess. I've set questions as well, so number 4 is out as well. Number 5, I've set the range from 0 to the number of all unanswered questions, above I've checked if they were any unanswered questions left, and if they weren't I've refreshed the list. So I don't think that would be a problem. And I don't have any SerializeFields not assigned, so it has to be 3. – thaiten Dec 18 '16 at 18:26
  • Do you mean with that, that I have to add the GameManager Script to every Game Object that has the Score Script on it? I'd need to fill in the Questions twice then, wouldn't I? – thaiten Dec 18 '16 at 18:28
  • GetComponent searches the current gameobject for a correct component. If you don't know where the component is you may use the static GameObject.GetComponent wich searches through the scene. I can't test it right now so the method name might be slightly different. – Noel Widmer Dec 18 '16 at 18:35
  • @thaiten The method is called: "GameObject.FindObjectOfType()" – Noel Widmer Dec 18 '16 at 18:39
0

any object can be null...

just check your code:

GetComponent<GameManager>();

can give you no GameManager,

questions.ToList<Question>();

or your list is not initialized. Fix it with:

 unansweredQuestions = new List<Question>();

You need to debug your code directly, or just check to != null

mty
  • 87
  • 9