Errors and new code broke my game [solved]

So i changed the code for the loop but the loop broke my game and nothing works.
also i think i followed the code correctly but i get 2 errors
First Error

NullReferenceException: Object reference not set to an instance of an object
AdventureGame.ManageStates () (at Assets/Scripts/AdventureGame.cs:28)
AdventureGame.Update () (at Assets/Scripts/AdventureGame.cs:23)

Second error

NullReferenceException: Object reference not set to an instance of an object
AdventureGame.Start () (at Assets/Scripts/AdventureGame.cs:18)

here is my code

AdventerGame.cs
`using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class AdventureGame : MonoBehaviour {

    [SerializeField] Text textComponet;
    [SerializeField] State startingState;

    State state;

	// Use this for initialization
	void Start () {

        state = startingState;
        textComponet.text = state.GetStoryState();
	}
	
	// Update is called once per frame
	void Update () {
        ManageStates();
	}

    private void ManageStates()
    {
        var nextStates = state.GetNextStates();
        for (int index = 0; index < nextStates.Length; index++)
        {
            if (Input.GetKeyDown(KeyCode.Alpha1 + index))
            {
                state = nextStates[index];
            }
        }

    }
}

and the states.cs script

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 GetStoryState()
    {
        return storyText;

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

}

trying to edit it but it won’t make the code in the box like i want it to

k fixed the look

Hi,

NullReferenceException means that a reference (“link”) to an instance is missing. If you exposed a field in the Inspector, make sure that it’s not empty. Check your AdventureGame component in your Inspector.

ok that fixed the errors thanks, but why is my loop not working after i made it to what was shown?

What does it do in your case?

what it does is when the game runs it will load the first start with the instructions to play. then it will not do anything when i press 1 or 2. maybe i should add a debug.log code so that when it changes states it will say a message to the console.

I think the issue is that you assign nextStates[index] to states but don’t do anything with the object. Unity does not automatically update the text in the text display unless you explicitely tell it to do that. See the second line in the Start method.

Thanks so I added textComponet.text = state.GetStoryState(); under
state = nextStates[index];
so i have the code
state = nextStates[index];
textComponet.text = state.GetStoryState();

in for loop and it works now thanks for the help and the hint

You’re welcome. :slight_smile:


See also:

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

Privacy & Terms