-1

I am running into an error that prevents my test character from moving on the screen. Could someone help me understand why this is happening?

Error:

NullReferenceException: Object reference not set to an instance of an object SimpleSampleCharacterControl.FixedUpdate ()

Code Line:

 private void FixedUpdate()
    {
        //fix the Character position when the NPC dialogue box is open.
        if(DialogueManager.GetInstance().dialogueIsPlaying)
        {
            return;
        }

Entire code: https://pastebin.com/t52U2mqD

Thank you for taking the time!

I was expecting my character to be able to move around the map and engage with NPCs

BLBug
  • 13
  • 2
  • Entire code on pastebin equals not posted. Relevent code must be provided in the question text because links can become stale. Posting the entire code is usually too much, anyway. Review the guidelines for providing a [mre]. – madreflection Feb 20 '23 at 20:18

1 Answers1

0

It appears the DialogueManager.GetInstance() call returns null, so referencing a field on it is causing an exception. I'm guessing the DialogueManager is a singleton, and that you forgot to initialise it, though I can't be sure without its code.

There are 2 options:

  • If DialogueManager is a Unity script, firstly check if you added the component somewhere in your scene. It's an easy mistake to make, and it's easy to waste time this way. If you checked that, ensure you're saving the instance to be publicly accessible, in Awake() or Start() (essentially check if you're implementing a singleton properly). It can look like this:
public class DialogueManager : MonoBehaviour {
    public static DialogueManager instance {get; private set;}
    
    void Awake() {
        // Only keep one instance. If one was already initialised before, destroy this component
        if (Instance != null) {
            Destroy(this);
            return;
        }
        instance = this;
    }
}

This code declares a static (single-instance, accessible without an object reference) field Instance that stores the current DialogueManager. The {get ; private set;} means that only the DialogueManager can set its value, while everything can read it. In Awake(), it sets that field to itself, making sure that there aren't multiple copies needlessly running. Wherever you want to access the DialogueManager (where you were previously calling GetInstance), you can just access the field Instance, like this: DialogueManager.Instance

  • If DialogueManager is not a script (it's just a class you have, not derived from MonoBehaviour), consider marking the class as static and updating the fields and methods respectively. Static is C#'s built-in way to create single-instance fields - they can then be referenced by just typing DialogueManager.fieldName anywhere in your code.
  • Thank you for your prompt help! Here is my dialogue manager script: https://pastebin.com/ZQwVxpFc Line: 44 ````private void Awake() { if (instance != null) { Debug.LogWarning("Found more than one Dialogue Manager exist in the scene."); } instance = this; dialogueVariables = new DialogueVariables(loadGlobalsJSON); } ```` – BLBug Feb 20 '23 at 20:31
  • @BLBug As suggested by Adam, have you verified that DialogueManager is attached as a component to an active GameObject in the scene? – Serlite Feb 20 '23 at 21:14
  • @BLBug that looks correct, can you ensure the script is attached to an active GameObject somewhere, as Serlite said? – Adam Wójcik Feb 22 '23 at 06:05