NullReferenceException: Object reference not set to an instance of an object

Greetings,
I get an error, when running “The.Touch” :slight_smile: I can see other references to the same error, in other posts, but i dont yet understand how all these elements hook into each other.
Basically i dont understand the error :slight_smile:
I have inclused the main game file and the states file.

NullReferenceException: Object reference not set to an instance of an object
TheTouch.ManageState () (at Assets/TheTouch.cs:30)
TheTouch.Update () (at Assets/TheTouch.cs:24)

states

Hello Lasse, welcome to the community :slight_smile:

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 :slight_smile:


See also;

1 Like

Hi Rob,
thank you, i had not added the starting state to the game object in the inspector :slight_smile: now it works.

As per your advice i have added the code as text so that its searchable, and i will do this next time im stuck aswell.
-lasse

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class TheTouch : MonoBehaviour {

     [SerializeField] Text textComponent;
     [SerializeField] State startingState;

    State state;


	// Use this for initialization
	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.Alpha1))
        {
            state = nextStates[0];
        }
        else if (Input.GetKeyDown(KeyCode.Alpha2))
        {
            state = nextStates[1];
        }
        else if (Input.GetKeyDown(KeyCode.Alpha3))
        {
            state = nextStates[1];
        }
        textComponent.text = state.GetStateStory();
    }
}

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;
    }
}
1 Like

Hi Lasse,

Great, glad that has resolved the problem for you and thank you with regards to the code. :slight_smile:

1 Like

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

Privacy & Terms