I’m getting this error when I run my game:
NullReferenceException: Object reference not set to an instance of an object
ChoiceButtons.selectedThisAnswer () (at Assets/Scripts/ChoiceButtons.cs:23)
Does anyone know why this is happening? Here are the two scripts:
public class ChoiceButtons : MonoBehaviour {
// Each button needs to have an ID so that we know which one they pressed
// as well as what to assign it.
[SerializeField] int choiceId;
[SerializeField] TextMeshProUGUI choiceText;
public int wrong = 0;
public GameManager gameManager;
GameoverScript gameover;
public void selectedThisAnswer(){
Debug.Log("Selected: " + choiceId);
//Do things with score and winning etc
**_if (choiceId == gameManager.randomChoiceButtonIDAsWinner)_**
{
Debug.Log("Correct");
Object.FindObjectOfType<GameManager>().ShowNewQuestion();
}
else
{
Debug.Log("Wrong");
switch (wrong)
{
case 0:
Image heart3 = GameObject.Find("heart3").GetComponent<Image>();
heart3.enabled = false;
wrong = 1;
break;
case 1:
Image heart2 = GameObject.Find("heart2").GetComponent<Image>();
heart2.enabled = false;
wrong = 2;
break;
case 2:
Image heart1 = GameObject.Find("heart1").GetComponent<Image>();
heart1.enabled = false;
gameover.Gameover();
wrong = 3;
break;
}
Object.FindObjectOfType<GameManager>().ShowNewQuestion();
}
//start new questions
//Object.FindObjectOfType<GameManager>().ShowNewQuestion();
}
public int GetChoiceId(){
return choiceId;
}
public TextMeshProUGUI GetChoiceText()
{
return choiceText;
}
}
public class GameManager : MonoBehaviour {
// This is the question's text
[SerializeField] TextMeshProUGUI questionText;
// This is the container that holds all of the choices. I've put this here so that we can loop through each button later
[SerializeField] GameObject choices;
public bool correct;
// these are the questions. I've labeled them like this so its easier to debug what the right answers are
string[] questions = { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
// These are going to be the answers. We could also combine these two arrays and make a dictionary instead. For now this will be fine though.
string[] answers = { "1", "2", "3", "4", "5", "6", "7", "8", "9" };
// This is going to hold our randomly selected question's position
int randomAnswerPositionInArray;
// This is going to hold our randomly selected choice button that is the winner
public int randomChoiceButtonIDAsWinner;
//Create an array to hold all of the answers. We are going to use this array to populate the choice text.
string[] textForChoiceButtons;
// Use this for initialization
void Start () {
ShowNewQuestion();
}
public void ShowNewQuestion(){
// Pick a random question
randomAnswerPositionInArray = UnityEngine.Random.Range(0, questions.Length - 1);
//Update the question's text
questionText.text = questions[randomAnswerPositionInArray];
//Choose a random button as the correct choice
randomChoiceButtonIDAsWinner = UnityEngine.Random.Range(0, 3);
}
There’s more but it’s not relevant. Thanks