State and linking issue

Sorry bare with me here…So I have been able to link the text however i am coming up with this issue here. While it runs, it gives me this error

Hey Ian,

Select your GameObject which has the script attached and then check the Inspector. A NullRefereneceException error is generated when you try to access an object which, is null, e.g. not instantiate or initialised.

If you havent dragged the relevant objects into the exposed fields on your script, this could be the cause.

Hope this helps :slight_smile:

Wish it did. Thanks for attempt… however not having luck resolving this issue. tried looking at the text… think i may just start over. again. Not seeing what i did wrong. i dont see where the instantiate or initialize would be. I see the boxes for state and text under having the script on there…

Hi Ian,

The boxes in the Inspector are your exposed fields from your CYOA script.

The TextComponent would should be fairly straight forward as this is another GameObject in your Hierarchy.

I havent looked at the updated content on these lectures recently but my understanding is that you use scriptable objects to create the different states. You would then be dragging the initial state object across into this field.

Having you started creating the scriptable objects yet, or even just a state script? I can’t really tell from your screenshot.

Thanks so much Rob, for being there to try to help me through this. It really has been bugging me… Right, new day today… I will resolve this issue… So last night I moved on past this issue. The error is coming from line 19 . which is textComponent.text = state.GetStateStory . Which its claiming doesn’t have an instance with an object.
There is no object on my hierarchy called textComponent.text. Anyways I will post a screenshot and my code for both scripts.

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

public class CYOA : 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()
    {

    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[CreateAssetMenu(menuName = "State")]
public class State : ScriptableObject {

    [TextArea(10, 14)] [SerializeField] string storyText;
    [SerializeField] State[] nextStates;

    public string GetStateStory()
    {
        return storyText;
    }

    public State[] GetNextStates()
    {
        return nextStates;
    }
}

Hi Ian,

Ok, so let’s break it down a bit…

NullReferenceException is going to mean that there isn’t a reference to an expected object, e.g. something you are trying to use hasn’t been instantiated.

On that line in question you potentially have two objects that could cause this;

  • textComponent
  • state

we can tell this because you are trying to access members (variables/properties/methods) from both of them on that line. In the case of textComponent you are trying to access it’s text property. In the case of state you are trying to access it’s GetStateStory method.

The textComponent is the variable that you have declared at the top of your class, whilst its private you have added the SerializeField attribute so that it appears in the Unity Inspector.

I am assuming that the CYOA script it attached to your Game GameObject in the Hierarchy. Assuming this is the case, select the Game GameObject in the Hierarchy and then view the Inspector on the right hand side. You may need to scroll down a little but you should see the CYOA Script Component, and, within that you will see two exposed fields, one named Text Component and one named Starting State.

These are where you would then drag and drop their corresponding objects, this is what creates the reference, and thus it would then not be null.

So, I would start by checking the above and seeing if you have made these references in those exposed fields. If you aren’t sure, select the Game GameObject in the Hierarchy and then take a screenshot displaying the Inspector and pop it up in a reply, I can take a look then.

If you are able to check this yourself and can confirm that these references have been made then we have some other steps to perform, so, let me know :slight_smile:

Incidentally, when I paste your first code example into a text editor, line 19 is actually this one;

state = startingState;

Would be good to confirm.


See also;

Thanks for the tip on posting my code. I was wondering about that.

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

public class CYOA : 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()
    {

    }
}

is that correct way?
here is screenshot
Screenshot%20(21)

1 Like

No worries, and yep, spot on - just makes it a little easier to read for everyone but also limits the size of the box and adds the inner scroll bar, so you can post lots of code and we can still see the rest of the post easily :slight_smile:

Thanks for the screenshot, so that’s looking good, you have the two objects dragged in…

Ok, so lets try the next step…

Let’s break apart your Start method and find exactly what’s going wrong…

Start by commenting one the exist lines of code;

void Start()
{
    // state = startingState;
    // textComponent.text = state.GetStateStory();
}

You should be able to run the game after this change, you obviously won’t see your story text change but at the same time, hopefully, we won’t see any errors.

Then, let’s build it up step by step until it fails…

Firstly;

void Start()
{
    state = startingState;
    // textComponent.text = state.GetStateStory();
}

Run the game, no errors?

Assuming all good, next;

void Start()
{
    state = startingState;
    // textComponent.text = state.GetStateStory();
    textComponent.text = "Hello Ian!";
}

Run the game… any errors, or just happy greeting?

Got an error that my CYOA.state is assigned yet never used in second step. Or first step depending on when you start counting

Now retried it and seems no errors

again did step three and this is my screenshot

That’ll just be a warning as opposed to an error I would imagine, typically your code will still compile and run.

Ok, so having run through all of the above, no errors… that’s handy… that suggests then that it’s the assignment of your; state.GetStateStory()

So, lets just check we’ll first put that line back in and see that we still get the original error;

void Start()
{
    state = startingState;
    textComponent.text = state.GetStateStory();
    // textComponent.text = "Hello Ian!";
}

Does the original error now re-appear?

Yah, its runs, it shows the text and links. It just shows this warning. Sorry to call it an error, when they are warnings. Just as it did with my original.

No, no, if it’s the NullReferenceException, then that is an error. Is that what you are seeing again, or are you seeing something else?

Its why i just decided to continue and move on last night was, my text is linked ( if i were to remove this // from the textcomponent.text = state.getstatestory). I can write in the scriptable object of startingstate and it comes up… just this warning keep coming up…

Yes nullreference is coming back even on just the (“hello ian”) text

Sorry Ian, I’m struggling to follow a bit here… are you saying that with this;

void Start()
{
    state = startingState;
    // textComponent.text = state.GetStateStory();
    textComponent.text = "Hello Ian!";
}

You still see a NullReferenceException error?

Yes, precisely.

its okay, communication can be tricky.

    {
        state = startingState;
        //textComponent.text = state.GetStateStory();
        textComponent.text = ("hello ian");

    }

Ok, tell you what, can you zip up your project files and share them with me, I think it’ll be a little easier to look at it directly.

The forum will allow uploads of up to 10mb, if your project files are larger than that, please use a service like Google Drive or Dropbox and share the URL with me.

I’ll take a look for you as soon as I can get the zip.

Privacy & Terms