Hello guys, I am creating a quiz game, this is my first game. It was working ok, up until this message appears:
NullReferenceException: Object reference not set to an instance of an object
Collectable.AnswerA () (at Assets/Scripts/MazeScripts/Areas/collect01/Collectable.cs:59)
I have read and analyze it, and tried to do some modifications. This line of code is what the error is pointing to:
ScoreManager.instance.IncreaseScore(amount: 3);
IncreaseScore
is method from another script called ScoreManager
. When the answer is correct I apply the score amount into the Inspector. Like this:
I have no idea what to do, so here both scripts.
public class Collectable : MonoBehaviour
{
//ScoreManager scoreManager;
public ParticleSystem particleSystemActivation;
public AudioClip activateSphere;
//Variables for the Digital Panel (OnTriggerEnter) and (OnTriggerExit)
public GameObject panelOnTrigger01;
public GameObject questionText01;
public GameObject btnAText;
public GameObject btnBText;
public GameObject btnCText;
public GameObject btnDText;
public GameObject btnA;
public GameObject btnB;
public GameObject btnC;
public GameObject btnD;
public int questionNumber;
public GameObject correctBip;
public GameObject wrongBip;
public bool correctAnswerVerification = false;
public GameObject correctTextBoard;
public GameObject wrongTextBoard;
public Animator portalSphereA1_1;
public ParticleSystem portalParticleSystemA1_1;
public ParticleSystem portalRipleA1_1;
public AudioSource voiceA1_1;
public GameObject reviewScroll;
void Start()
{
//scoreManager = GameObject.Find("Canvas").GetComponent<ScoreManager>();
}
public void AnswerA()
{
correctBip.GetComponent<AudioSource>().Play();
correctAnswerVerification = true;
ScoreManager.instance.IncreaseScore(amount: 3);
//scoreManager.IncreaseScore();
particleSystemActivation.Play();
gameObject.GetComponent<AudioSource>().Play();
correctTextBoard.SetActive(true);
wrongTextBoard.SetActive(false);
panelOnTrigger01.SetActive(false);
btnA.GetComponent<Button>().interactable = (false);
portalParticleSystemA1_1.Play();
portalSphereA1_1.SetBool("OpenPortal", true);
portalRipleA1_1.Play();
voiceA1_1.Play();
reviewScroll.GetComponent<ReviewList>().AddQuestionAndAnswer
(question: "<color=white>Very accurate ", answer: "<color=white> Exact");
}
public void AnswerB()
{
wrongBip.GetComponent<AudioSource>().Play();
correctAnswerVerification = false;
wrongTextBoard.SetActive(true);
correctTextBoard.SetActive(false);
btnB.GetComponent<Image>().color = Color.red ;
btnBText.GetComponent<TMP_Text>().color = Color.red;
btnB.GetComponent<Button>().interactable = (false);
}
public void AnswerC()
{
wrongBip.GetComponent<AudioSource>().Play();
correctAnswerVerification = false;
wrongTextBoard.SetActive(true);
correctTextBoard.SetActive(false);
btnC.GetComponent<Image>().color = Color.red;
btnCText.GetComponent<TMP_Text>().color = Color.red;
btnC.GetComponent<Button>().interactable = (false);
}
public void AnswerD()
{
wrongBip.GetComponent<AudioSource>().Play();
correctAnswerVerification = false;
wrongTextBoard.SetActive(true);
correctTextBoard.SetActive(false);
btnD.GetComponent<Image>().color = Color.red;
btnDText.GetComponent<TMP_Text>().color = Color.red;
btnD.GetComponent<Button>().interactable = (false);
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
panelOnTrigger01.SetActive(true);
questionText01.GetComponent<TMP_Text>().text = "Very accurate";
btnAText.GetComponent<TMP_Text>().text = "A. Exact";
btnBText.GetComponent<TMP_Text>().text = "B. Fundamental";
btnCText.GetComponent<TMP_Text>().text = "C. Efficient";
btnDText.GetComponent<TMP_Text>().text = "D. Conscious";
//expectedAnswer = "A. Exact"
}
}
private void OnTriggerExit(Collider other)
{
panelOnTrigger01.SetActive(false);
}
public class ScoreManager : MonoBehaviour
{
public static ScoreManager instance;
[SerializeField] TextMeshProUGUI scoreText;
[SerializeField] TextMeshProUGUI prepScoreText;
int score;
int scorePrep;
// Start is called before the first frame update
void Start()
{
scoreText.text = "Don't use very - Panel: " + score + "/40";
prepScoreText.text = "Preposition Challenge - panel: " + scorePrep + "/15";
}
public void IncreaseScore(int amount)
{
score += amount;
scoreText.text = "Don't use very - Panel: " + score + "/40";
}
public void IncreasePrepScore()
{
scorePrep++;
prepScoreText.text = "Preposition Challenge - panel: " + scorePrep + "/15";
}
I’ll gladly provide more data if someone of you analyzing my case, ask for it, if necessary. I am in panic now LOL.
Thanks.