Hello Lasse, welcome to the community
A NullReferenceException
error basically means that the object you are trying to reference is, null, as opposed to being a reference to an object, an object being an instance of a class for example.
So, your error message is pointing, I believe (see below), to this line of code;
var nextStates = state.GetNextStates();
Your state
object is being initialised within the Start
method, where you are saying that it is equal to the startingState
, if startingState
is null, then state
would be also. startingState
is delcared at the top of your class a member variable, a variable belonging to the class. You have added the [SerializeField]
attribute because you want to expose this otherwise private field to the Inspector.
So, following this issue backwards, we have arrived at the Inspector. Select your GameObject, view the Inspector and see if you have created a reference for your startingState
by dragging the scriptable object into this exposed field.
With regards to my see below
comment earlier…
Screenshots are really useful for error messages and details from within the Unity editor, but are not so useful for code. For code, please considering just copy/pasting your code directly into your post and then applying the code formatting characters before/after it.
There are several benefits by doing so;
- readability, it’s a lot easier to read the text as text, rather than as a screenshot.
- time-saving - for those that give up their time to offer help it enables them to copy/paste parts of your code back to you with suggestions/corrections, without having to type it all out - this means it’s quicker for them and will invariably increase your likelihood of getting replies.
- searchability - the text will also be searchable on the forum, so other people searching for answers to similar problems will be more likely to find your posts and their answers.
Hope this helps
See also;