I copied every bit of code in the “Manage Next States video”
//States
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(menuName = “State”)]
public class State : ScriptableObject
{
[TextArea(14, 10)] [SerializeField] string storyText;
[SerializeField] State nextStates;
public string GetStateStory()
{
return storyText;
}
public State[] GetNextStates()
{
return nextStates;
}
}
//AdventureGame
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class AdventureGame : MonoBehaviour
{
[SerializeField] Text textComponent;
[SerializeField] State startingState;
State state;
// Start is called before the first frame update
void Start()
{
state = startingState;
textComponent.text = state.GetStateStory();
}
// Update is called once per frame
void Update()
{
ManageState();
}
private void ManageState()
{
var nextStates = state.GetNextStates();
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
state = nextStates[0];
}
else if (Input.GetKeyDown(KeyCode.RightArrow))
{
state = nextStates[1];
}
textComponent.text = state.GetStateStory();
}
That messages means that you are trying to access data that does not exists (null = nothing), for instance, let’s say you have a code that moves an object, if the object doesn’t exist it will throw that same error, fix it by simply asigning that value to something that actually exists in the current context.
I’ll teach you how to read that message error so you can easily fix it next time, and believe me, you’ll encounter that error message a lot.
(at Assets/AdventureGame.cs:29)
at = in, at this folder.
Assets = Assets folder.
AdventureGame.cs = Script with that name.
29 = The line the error is in.
Based on that information you can see that your script “AdventureGame” is trying to access and object that doesn’t exist in line 29, which is:
private void ManageState()
{
var nextStates = state.GetNextStates(); //<--- THIS LINE HERE
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
state = nextStates[0];
}
else if (Input.GetKeyDown(KeyCode.RightArrow))
{
state = nextStates[1];
}
textComponent.text = state.GetStateStory();
}
That means that your variable “state” has not been set properly.
I really suggest you try to fix this by yourself by this point, just follow the breadcrums and you’ll find the error, but if you still need help I’ll explain further.
This are the breadcrums:
“state” is set in the method “Start”.
“state” is set to startingState.
“startingState” is set in the editor, we know that because of that “[SerializeField]” modifier at the start of the variable.
Identify which game object has the AdventureGame script attached.
Select the game object and see if the variable “startingState” (in the editor the name will be “Starting State”), has been set to something, if not then set it to one of your scriptable objects you create previously.
This should fix your problems, if not, keep asking, we’ll eventually figure it out.