Number Wizard UI Null Reference

I’m only into the Number Wizard UI for Unity 2d, and its the first course I’ve taken, so please be gentle. I have tried to add some additional functionality to the game wherein I have scriptable objects that give the wizard some flavor text, and have a unique image assigned to each text bit. The code works within Unity, except it throws a null reference exception, and when built for WebGL it throws a memory access out of bounds error when you click the “start” button to move into the game. Can someone help me with this in a new-dude-to-this speak? Can that even be done? Thanks in advance for your help!

The Scriptable objects script BMchat.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

[CreateAssetMenu(menuName = "chat")]

public class BMchat : ScriptableObject
{
    [TextArea(14, 14)] [SerializeField] string bmChat;
    [SerializeField] Sprite bmImage;
    public string Chat()
    {
        return bmChat;
    }
    public Sprite Sprite()
    {
        return bmImage;
    }
}

My MainGame.cs

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

public class MainGame : MonoBehaviour
{

    [SerializeField] int maximum;
    [SerializeField] int minimum;
    [SerializeField] TextMeshProUGUI guessText;
    [SerializeField] Text textLink;
    [SerializeField] BMchat[] chatmad;
    [SerializeField] Image imagelink;

    Sprite bmSprite;
    string angrychat;
    int arraysize;
    int playerGuess;
    void Start()
    {
        maximum = maximum + 1;
        NextGuess();
        textLink.text = ("This is your number or I will burn everything you have ever loved.");
        for (arraysize = 0; arraysize < chatmad.Length; arraysize++);
    }

    public void Higher()
    {
        minimum = playerGuess;
        NextGuess();
        AngryChat();
    }

    public void Lower()
    {
        maximum = playerGuess;
        NextGuess();
        AngryChat();
    }
    public void NextGuess()
    {
        playerGuess = Random.Range(minimum, maximum);
        guessText.text = playerGuess.ToString();
    }
    private void AngryChat()
    {
        int randomizer = Random.Range(0, arraysize);
        angrychat = chatmad[randomizer].Chat();
        bmSprite = chatmad[randomizer].Sprite();
        textLink.text = angrychat;
        imagelink.sprite = bmSprite;
        
    }
}

And my SceneController.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class SceneController : MonoBehaviour
{
    public void NextScene()
    {
        int currentScene = SceneManager.GetActiveScene().buildIndex;
        SceneManager.LoadScene(currentScene + 1);
    }
    public void ReturnScene()
    {
        SceneManager.LoadScene(0);
    }
}

The Maingame script is attached to an empty object and the object references are filled as such

Hi,

First of all, it’s great to see that you are trying to implement your own ideas. :slight_smile:

NullReferenceException is one of the most common errors a programmer gets. It means that a reference (“link”) to an instance is missing. If you exposed a field in the Inspector, make sure that it’s not empty. When you get this error, pause the game and check the fields.

If all fields in your Inspector contain references, double click on the error message to learn to which line in your code it refers. There is very likely a variable that is supposed to contain a reference to an object. It’s not a number or a char because those are value types.

In the Start method of your MainGame.cs, there is a for loop without any code block. If it does not have any purpose, remove it.

At the moment, I don’t know what could be causing the out of memory issue, so I’d recommend to fix the problems in Unity first before you try to fix the WebGL build.


See also:

Thank you so much for your reply! In the inspector my MainGame.cs is attached to an empty object, and the inspector shows all of the fields filled. But if I click on the script itself, it shows those fields empty, with no options for attaching them to a game object. I wasn’t sure how to assign them directly in the code since they were assigned in the inspector.

Hi Have you found a solution to this?
Did you try with the game paused as suggested?
The error sounds like its searching beyond the length of the array or buildIndex

Sort of. VisualStudio suggested a fix for some of the issues, but it’s still throwing a Null Reference on line 70, which is

        GuessText.text = PlayerGuess.ToString();

My full code and the way that VS fixed it is below.

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

public class MainGame : MonoBehaviour
{

    [SerializeField] int maximum;
    [SerializeField] int minimum;
    [SerializeField] TextMeshProUGUI guessText;
    [SerializeField] Text textLink;
    [SerializeField] BMchat[] chatmad;
    [SerializeField] Image imagelink;

    Sprite bmSprite;
    string angrychat;
    int arraysize;

    public Text TextLink
    {
        get => textLink;
        set => textLink = value;
    }
    public TextMeshProUGUI GuessText
    {
        get => guessText;
        set => guessText = value;
    }
    public BMchat[] Chatmad
    {
        get => chatmad;
        set => chatmad = value;
    }
    public Image Imagelink
    {
        get => imagelink;
        set => imagelink = value;
    }
    public int PlayerGuess
    {
        get;
        set;
    } = 0;

    void Start()
    {
        NextGuess();
        TextLink.text = ("This is your number or I will burn everything you have ever loved.");
        for (arraysize = 0; arraysize < Chatmad.Length; arraysize++);
    }

    public void Higher()
    {
        minimum = PlayerGuess + 1;
        NextGuess();
        AngryChat();
    }

    public void Lower()
    {
        maximum = PlayerGuess;
        NextGuess();
        AngryChat();
    }
    public void NextGuess()
    {
        PlayerGuess = Random.Range(min: minimum, max: maximum + 1);
        GuessText.text = PlayerGuess.ToString();
    }
    private void AngryChat()
    {
        int randomizer = Random.Range(0, arraysize);
        angrychat = Chatmad[randomizer].Chat();
        bmSprite = Chatmad[randomizer].Sprite();
        TextLink.text = angrychat;
        Imagelink.sprite = bmSprite;
        
    }
}

Check if guessText is null.

How are you getting on with this, @TheBarhat?


See also:

Privacy & Terms