This lesson confused me a bit

I was understanding till we got to the part on vid 48 [Complete C# Unity Game Developer 3D].
When we got to the bool isTransitioning = false; and then put it in the void StartSuccessSequence() and void StartCrashSequence() as isTransitioning = true and then put an if statement up in void OnCollisionEnter(Collision other) as if (isTransitioning) { return; }

So we marked StartCrashSequence and StartSuccessSequence as true in the switch code. The if statement is where I actually get confused on.

So in the if statement it’s saying if isTransitioning is true then return but they are already true in the crash and sucessSequence. So yeah I’m confused haha.

void OnCollisionEnter(Collision other)

{

   if (isTransitioning) { return; }

   switch (other.gameObject.tag)

   {

        case "Friendly":

            break;

        case "Finish":

            StartSuccessSequence();

            break;

        default:

            StartCrashSequence();

            break;

    }

}

Hi Zhaine,

What exactly would you like to know? What is not clear yet?

The if statement I don’t understand. If StartSuccessSequence and StartCrashSequence is true in the switch statement then what exactly does the if statement mean? if isTransitioning is true then return? They are already true. I think I’m confusing myself more haha.

if (isTransitioning) { return; } means: If isTransitioning is true, terminate the method immediately and do NOT execute the rest of the code block, here: the switch. That’s all.

We “need” that if-statement to be on the safe side because our rocket could collide with something triggering the OnCollisionEnter while isTransitioning is true. In that case, we don’t want to call StartSuccessSequence or StartCrashSequence (again).

Does that make sense? :slight_smile:

So when we crash something it becomes true and that’s when the if statement takes over since it already happened.

The first time we crash, StartCrashSequence gets called, and isTransitioning gets set to true. If the OnCollisionEnter method gets called again, nothing is supposed to happen. So, yes, we could say that “the if statement takes over”.

So in Void StartCrashSequence us putting the code isTransistioning = true; isn’t actually saying it’s true? It’s actually false untill StartCrashSequence gets called up in the switch statement? Sorry lol. Who knew programming could be soo confusing… Cough Cough… XD

Ahhh, thanks to a friend he helped me understand. Not sure why I didn’t get this until now :. We call isTransitioning as false, and place it in void crash and successSequence as true. Once, in the switch statement, it’s called it goes from false to true there for the if statement takes affect because it is now true which in turn stops it from doing it again.

1 Like

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

Privacy & Terms