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.