(Spoiler! -Watch course first) Small contribution, to avoid errors down the road

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
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! :smiley:

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.

1 Like

Thank you for the tip! This will help a lot of people :ok_hand:

1 Like

You’re welcome!
Thank you as well :smiley:

Hello, i will try that as i do get the same errors you mentioned above.

Thank you!

image

Turns out on my end only button 1 changes them all 2 only works a the starting state and 3 doesn’t work from room 2… and unity bugs out after i press buttons couple of times doesn’t work at all.

I’ll have to redo my project because of a double HDD failure (main+backup got fried) so i can’t check mine aside of that code on the top.

What i can recommend you is to follow the videos 1 to 1, trying on your own when he says about an exercise, then comparing what you did to what he shows in order to locate the problem

You’ll also see that in the next lessons he goes over what i mention in this post as well, so you might be more comfortable following it in the order he did (i’ve posted this before reaching that other lesson).

I hope this helps, cheers! :slight_smile:

1 Like

Privacy & Terms