Rooms assigned to the same key. Using Unity 2019.3.13f1

Hi all,

I have just finished the session “manage next states” and unfortunatelly came up with a new problem.
When i try to cycle the rooms in unity for some reason the states are assigned to the same key… For ex if i am in room 1 and press key1 it takes me to room 1, if i press it again it takes me to room 2, if i press key2 it takes me to Starting Room and if i press it again it takes me to room2. The same goes with all rooms.

Another thing i noticed is that in the video when Rick starts the game he can go over between rooms without having to stop the game. I can’t. If i am in room 1 and want to see if the cycle works for the other rooms, i have to stop the game, choose room 2 hit play and so on.

Can someone help me?
Thank you all again for your time

photo 00 photo 01

P.S. Excuse my english, but this is not my native language

Hi.

If you format the code like this you may get people more willing to help. Here is my code. It works fine. Yours looks like it’s OK. It sounds like the problem is in the inspector.

I would check that.

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

public class AdventureGame : MonoBehaviour
{

    [SerializeField] Text textComponent;
    [SerializeField] State startingState;

    State state;

    // Start is called before the first frame update
    void Start()
    {
        state = startingState;
        textComponent.text = state.GetStateStory();
    }

    // Update is called once per frame
    void Update()
    {
        ManageStates();
    }

    public void Quit()
    {
        #if (UNITY_EDITOR || DEVELOPMENT_BUILD)
            Debug.Log(this.name + " : " + this.GetType() + " : " + System.Reflection.MethodBase.GetCurrentMethod().Name);
        #endif
        #if (UNITY_EDITOR)
            UnityEditor.EditorApplication.isPlaying = false;
        #elif (UNITY_STANDALONE)
            Application.Quit();
        #elif (UNITY_WEBGL)
            Application.OpenURL("about:blank");
        #endif
    }

    private void ManageStates()
    {
        State[] nextStates = state.GetNextStates();

        if (Input.GetKeyDown(KeyCode.Q))
        {
            Quit();
        }

        for (int index = 0; index < nextStates.Length; index++)
        {
            if (Input.GetKeyDown(KeyCode.Alpha1 + index) || Input.GetKeyDown(KeyCode.Keypad1 + index))
            {
                state = nextStates[index];
            }
        }
        textComponent.text = state.GetStateStory();
    }
}

Hi,

Thank you for your response.
I didn’t solve the problem in the testing format of the game (i am sorry for my English), but when i moved on to creating the game with all the states, it worked fine. I don’t understand why but somehow it solved itself.
I will keep in mind your suggestion. I don’t know how to copy paste in this format the code, but i will find it.

Thank you again
Stay safe and keep being creative

Best regards
Dimitris

I am glad it worked out for you.

Here is a post on how to format the code. Nice and easy. It’s basically just three ` and three ` at the end of the code.

You too. Keep being creative.

Privacy & Terms