Problem with the nullreferenceEception: Object reference not set to an instance of an object

Untabc

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;

	// 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[2];
        }
        textcomponent.text = state.GetStateStory();
    }
}
1 Like

Hi Mayank,

Check that you have dragged the appropriate state script able objects into the exposed field for each option. Missing them is typically the main cause of this error in this section of the course.

Hope this helps.


See also;

Exposed field means?

A null pointer exception or null reference exception occurs when you are attempting to access an object that is null. In this case, your attempts to call state.GetStateStory() in your start function and state.GetNextStates() in ManageState. You can tell this is where it’s dying by the line numbers in the error messages in Unity (AdventureGame.cs:17 and AdventureGame.cs:30 so lines 17 and 30).

What this is telling you is that state is null when those lines are executing. The assumption would then be that you have not attached an initialized State object to the StartingState field of the script in the Unity editor.

How do i do that? I mean, attach state object to the startingState field.

I’m sorry, I haven’t taken this course, so I can’t tell you exactly, but I would back up the video a bit and look for a point where they are dragging and dropping an object onto a field on the right hand side with the label Starting State next to it.

When [SerializeField] is used, that means you are exposing that variable to the Unity editor, and it is expecting an object to be attached to that field by dragging it in or selecting it with the little button next to the field.

I watched the tutorial again, I have done exactly the same as told while dragging and dropping the objects, I can’t find the error I am making, I am stuck here, Please, can anybody help me with this?

Well your issue inspired me to just go ahead and buy this course to see if I could reproduce it.

Starting state is attached to the “Game” object in lesson 24 at around the 6:45 mark of the video. Check your “Game” object, and ensure that your “StartingState” scriptable object is attached to the AdventureGame script. It should look like this:
image

2 Likes

Okay I agree I am dumb, It wasn’t attached ,that was the error . And just for helping me you bought the course, I am really thankful to you for that, Thanks a lot.

1 Like

Okay I agree I am dumb, It wasn’t attached, that was the error.

It’s not about being dumb Mayank, you are on a learning journey, you cannot expect to know everything straight away. Even if something has been shown to you in a lecture once, until you have performed that task several times over and over it won’t necessarily be concrete.

Try not to be hard on yourself. Instead, consider the fact that after Mike gave you a suggestion to the issue and how to resolve it you now have a better understanding than you did previously, and perhaps, the next time a similar issue arises you may think back to this experience and find and resolve the issue yourself - that’s learning :slight_smile:

2 Likes

As Rob said, don’t be hard on yourself. I’ve been a professional software engineer for about a decade and I make little mistakes all the time. We all do. That’s the reason we do code reviews when checking in code. We always want someone else’s eyes on the code to catch our frequently little, and occasionally huge mistakes.

1 Like

Privacy & Terms