Help! My game is not looping properly

cud_s03_organise_state_files

#1

I am confused – I tried to follow the directions but somehow I must have gotten messed up – when trying to run my game, it doesn’t loop properly. I just keep getting this error:


#2


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();
}

}


#3

If I had to bet this is not a problem with the code but an issue with Unity. You probably forgot to assign back in Unity the “Text Component” or the “Starting State” instance (better called reference).

Capture


#4

Do you have 3 nextStates set on each of your States? I went with 2 to simplify things at this stage. See below.
EDIT: Ignore Action Text, Header Text and Image. That’s just bits I’ve added on as a personal challenge.

private void ManageState()
{
    var nextStates = state.GetNextStates();

    if(Input.GetKeyDown(KeyCode.Alpha1) || Input.GetKeyDown(KeyCode.Keypad1))
    {
        state = nextStates[0];
    }
    else if (Input.GetKeyDown(KeyCode.Alpha2) || Input.GetKeyDown(KeyCode.Keypad2))
    {
        state = nextStates[1];
    }
}

image

image

image


#5

Hey Horusofoz,

I followed the instructions according to what the instructor said because I didn’t want to take a chance on being messed up later on since I don’t know what he is going to have us do later.

I was actually working on it again last night and now I got it working (crossing fingers). I don’t remember exactly what it was that fixed it – I just went through it again state by state and making sure my states were leading to where I designated them to go according to my flow sheet.

Thanks for your help


#6

My text component is the same as yours “Story Text.” My StartingState is the Introduction to the Game like he has to create.

As far as I know, it is working now after having gone back through last night state by state making sure that each one was going to where it was supposed to go on flow sheet.

Thanks