Hello! First of all very nice course, i love it and allowed me to get back on track with Unity (i had some experience with it and C#, but been 3 years since last I’ve used it).
This is my code version of the states, you can see that before doing the scene swap I’ve added a additional “if”, and what that does is checks for the amount of vaiables in the array. What this prevents is getting errors like the one of this image
where my scene has only 1 action (proceed forward) but the keys are still active for the other options.
In example: i will not always have three scenes, so to prevent errors i can add that. This basically means that if I have two options instead of three, it will look at the array lenght (or array’s total element count) and since it will be shorter than required, it won’t process it to avoid errors.
private void ManageState()
{
State[] nextStates = state.GetNextStates();
if (Input.GetKeyDown(KeyCode.Alpha1))
{
if(nextStates.Length >= 1) state = nextStates[0];
}
else if (Input.GetKeyDown(KeyCode.Alpha2))
{
if(nextStates.Length >= 2) state = nextStates[1];
}
else if (Input.GetKeyDown(KeyCode.Alpha3))
{
if(nextStates.Length >= 3) state = nextStates[2];
}
UpdateText();
}
private void UpdateText()
{
_advText.text = state.GetStateStrory();
}
Edit:
if(nextStates.Length >= 2) state = nextStates[1];
is the same as (below), but since it’s just one command there’s no need for putting it between {} so I’ve decided to explain that just in case:
if(nextStates.Length >= 2)
{
state = nextStates[1];
}
I might be jumping ahead of myself here, but i believe it was a nice moment to mention this.
I hope it helps!
Also as a side note, I’m using the latest Unity version, which is Unity 2020 LTS (2020.3.14f1). Which so far I’ve had no issues with, but if you’re not used to it definitely stick with the course’s version for support’s sake.