How to null element / key press in for loop

So I have been going through the course and have done the for loop as seen below. What I want to do is either use my return / enter key as well for options within my game. So basically I want to null element zero preferably as this would just look nicer when there is no state set to it and make elements 1 and up my for loop. I have tried many different ways and even tried making say element 3 or 4 my return option as well and I keep getting the Object reference not set to a instance error when a incorrect key is pressed. I have included the code and screenshots of what I am talking about.
The only way I seen to do this after hours of trying to figure it out on my own was to ad a replica state in element zero, example ( I am in room 2 so I put room 2 in element zero ). This basically just sends me right back to where I am of course and works but is not what I am trying to accomplish. I want to be able to null the key press when there is no state in the element.

Any help is greatly appreciated.

private void ManageState()

{
    var nextStates = state.GetNextStates();

    for (int index = 0; index < nextStates.Length; index++)
    {
        if (Input.GetKeyDown(KeyCode.Alpha1 + index))

        {
            state = nextStates[index];
        }

    }            
    textComponent.text = state.GetStateStory();
}

}

Hi,

This is how you could check for null:

if (state == null)
{
     // your code when there is no object assigned to state
}

Is this what you were looking for?

I will definitely try this when I get home tonight. I will give a update then.

Thank you,

This is seeming to not work either. What I am looking for is to use Return as my element 0 as a option in some states but not in others. So I need to be able to void it out when not in use. Every time it is pushed when not in use it brings up the error. This goes the same vise versa with the for loop when I have return set as say element 3. the loop will bring up the error. Using the null all I was able to do was stop any actions from happening at all. Here is my code with the if added with return. This is the only way I have found it to semi work but still gives the error.

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.Return))
    {
        state = nextStates[0];
    }

    for (int index = 1; index < nextStates.Length; index++)
    {
        if (Input.GetKeyDown(KeyCode.Alpha0 + index))
        {
            state = nextStates[index];
          
        }
       

        textComponent.text = state.GetStateStory();
    }
}

}

Thank You,

I think I might have misunderstood your idea. Could you explain it again?

In your code, I can see that you added an if-statement. When the player presses the Return key, state = nextStates[0]; gets executed. That makes sense so far. However, what is supposed to happen afterwards?

NullReferenceException means that a reference (“link”) to an instance is missing. Double click on the error message to see to which line in your code it is referring. Since Update gets called each frame, ManageState and the following line get executed each frame, too:

var nextStates = state.GetNextStates();

If state got set to null in the previous frame, that line will throw a NullReferenceException in the next frame.

The value of Element 0 in your screenshot is null.

image

What I don’t understand yet is: Why do you set state = nextStates[index];? And what is supposed to happen if the value of state is null?

So in my story some pages are gotten to by pushing return as seen below.

On the next page the element 0 ( return ) is not exsiting as seen in this next picture below.

So I push return and it takes me to the next page which doesn’t have a element 0 (return) as a option to use but rather 1 or 2.

If I hit return on the second page it brings up this error. The error lines refer to the Managestate line at the top and the textComponent line at the very bottom.

image

setting nextStates to index was what was shown in the video to cause the loop I tried changing that to [2] and changing the return to function [3] but it pulls up the same error.

I hope this makes it clearer.

Sorry this should be the first picture not the one above.

To simplify it I want to use return as element 0 on the pages where return is the option. In doing so I cannot use element 0 on the other pages because It is assigned to return and my options are 1,2,or 3. I don’t have to null it necessarily but I want it to work on the pages enter is needed and not on the ones it is not needed/

How can Unity know when the return key is available? Theoretically, the player could press the return key whenever they want because there is no restriction in ManageState.

Perhaps you could add a bool variable to the State class and check the value in your AdventureGame instance. The bool variable could be named “hasReturnOption” or something like that.

This is how you can combine conditions: if (A && B) { }
&& means “and”.

I actually tried that last night and again today thinking I missed something, even had my brother help me and we couldn’t figure it out. Right now I am going to copy everything and make a second file and I am adjusting the first one two only use the alpha numbers with changing enter to 1 and then option 1 on the screens where enter is not used will be 1 as well. It is the only work around I have found so far but I am not giving up and will continue to work on the other copy until I figure it out. I think I will continue on with the course for now and work on it sporadically otherwise it will burn me out and I will learn more and maybe the answer will just hit me.
I will keep you updated if this gets solved before this post closes or if anyone else has any ideas as well.

Thank you for all the help. Here is what I ended up doing for now.

I changed Push Enter to Continue Push 1 to Continue instead.

changing Enter/Return to 1 I just removed the last element as 1 is element 0 now instead of Enter/Return which is what I was wanting element 0 to be and was trying to bypass and ignore in the states it wasn’t being used in without bringing up the NullReferencException error.

That’s a nice workaround in my opinion. I’m sure, if you proceed with a different project and come back to this problem in a couple of weeks, you’ll suddenly have a different idea or you will see a flaw in your current concept. When we spend too much time with a problem, we often start suffering from tunnel vision and miss things we otherwise would not miss.

That being said, keep your current solution for now or forever. The player will not miss the return key feature because they don’t know about your plans. As long as the game is entertaining, playable and bugfree, they are happy. :slight_smile:

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

Privacy & Terms