Numberwizard.cs script non random number guess script

it showing an error in line no 24 on my visual studio as NullReferenceException: Object reference not set to an instance of an object
NumberWizard.StartGame () (at Assets/scripts/NumberWizard.cs:24)
here is my code, which is already provided in your lectures resource folder:-

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;

public class NumberWizard : MonoBehaviour
{

[SerializeField] int max;
[SerializeField] int min;
[SerializeField] TextMeshProUGUI guessText;

int guess;

// Use this for initialization
void Start()
{
    StartGame();
}

void StartGame()
{
    guess = (max + min) / 2;
    guessText.text = guess.ToString();   // this part causing the error
    max = max + 1;
}

public void OnPressHigher()
{
    min = guess;
    NextGuess();
}

public void OnPressLower()
{
    max = guess;
    NextGuess();
}

void NextGuess()
{
    guess = (max + min) / 2;
    guessText.text = guess.ToString(); // this part causing the error 
}

}

please help me to solve this. Although game is running as per expected logic but this error keep appearing on my unity console while i’m running the game.

Did you drag the the text mesh pro “Guess Text” object to the Gameplay script component as shown in the video starting at minute 2:30?

For those who are still running into this issue and could not find the answer;

First make sure you dragged the TMPro text into the Serialized Field on the Gameplay object. (obvious)

In prior lessons we attached the AdventureGame.cs script to the “Main camera”, and after that you probably duplicated that scene.

So double check the “Main Camera” in every scene, and remove the “AdventureGame.cs” script from there (since the script is now attached to an empty gameobject)

Don’t forget to save the scenes, and after that it should work.

Privacy & Terms