Unity says my fields are all null, How do I fix this?

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();

}

}

What the heck is null and why?

I think I know what is going on and it’s nothing to worry about, but I’ll need to see your error message first, take a screenshot of the console.

Oh, that’s not what I was thinking but worry not.

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.

Sin título

(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.

My Starting State was already set before you told me to do it.

Never mind, that actually WAS the answer I had Adventure game in the “Game” file also where it was not set,

I can’t believe the answer was as simple as that. How embarrassing. XD

1 Like

You don’t have to be embarrased, that actually happens a lot.

An easy way to detect that kind of issue is by using this sort of code:

print(gameObject.name);

That way you can tell if your code has multiple instances in the scene.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms