Q: Manage Next States

I got these bad guys

My code seems to be all good, and I even tried copying and pasting from the github project but still got the same problem.

Here my looks inside my Unity

imagen imagen
imagen

Here my code (ignore the comments u.u)


using UnityEngine;
using UnityEngine.UI;

public class AdventureGame : MonoBehaviour
{
    [SerializeField] Text textComponent;
    [SerializeField] State startingState;   //Now we need a way to continuely now what State we are in.

    State state; //Aqui solo tenemos una variable de tipo State, tiene acceso a las funciones de esa clase.
    // [SerializeField] State startingState; crea un espacio que se puede observar en el scrip en el inspector
    //aunque al pasar allá queda Unite lo organiza así Starting State :____________ luego en ese espacio en blanco
    //se asigna el scrip del que hereda las funciones.
    
    void Start()
    {
        state = startingState; //aqui definimos state para que haga referencia a los datos que contiene startingState
                               //startingState Contiene la historia. 
        textComponent.text = state.GetStateStory();
    }

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

    private void ManageState()
    {
        var nextStates = state.GetNextState();
        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();
    }

}
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[] GetNextState()
    {
        return nextStates;
    }
}

Hope it’s not a problem with unity a few days ago it was arguing that he was read-only, but then give it administrator powers to both the hub and the black unity icon, and then it was just fine.

Help me pls I’m dying here :sob:

I’ve found what seems to be the problem.
imagen
If I click the scrip it appears like this…

but if I click the “Game” object …
imagen

And then it shows this error

I’m still looking for a solution, currently I’m rewatching the videos.

imagen
This took care of those yellow lines.

But this one persists

Okay, at last, I was able to give it a solution.

Here was the problem.
imagen

and this is the solution
imagen

Now I feel kinda dumb hahaha, so the problem was caused cuz I didn’t give the other options, it was like saying to Unity the answer is number 2 but then Unity just had one option.

In other words, I was overextending the array, it had just [1] element assigned but then I was asking for other elements, say [2]…

You have to assign the other elements of the array to the others ScriptableObject as I did with Room 2.

Actually, you shouldn’t feel dumb because you debugged your game and successfully fixed it. Even the best programmers make “stupid” mistakes all the time. For this reason, every programmer needs solid debugging skills.

Keep up the good work! :slight_smile:

1 Like

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

Privacy & Terms