Cant understand why is my logic not working?

So this is the logic :

//here we click the mouse we launch the ball

and then change now the hasStarted to true so the If statement is locked and cant be executed again.

hasStarted = true; // but its not working.

void Update()

{

if (hasStarted == false) // 1)If hasStarted == false then execute the if statemen

    {

        LockBallToPaddle();     // 2) we lock the ball

        LaunchOnMouseClick();   // 3) We press the MouseButton and the ball launches 

        hasStarted = true;    // 4)  We change the bool so dont execute again the if statement 
                                                 again(BUT its not working)

    }

}

private void LaunchOnMouseClick()

{

    if (Input.GetMouseButtonDown(0))

    {

       // hasStarted = true;   //<---- working like in the course

        GetComponent<Rigidbody2D>().velocity = new Vector2(xPush, yPush);

       // hasStarted = true;   <---- if i put here is still working and it changes after we launch the ball.Why is working here and  in Update NO????? it still executes after the ball was launched.



    }

}

Hi Shaktillar,

Welcome to our community! :slight_smile:

Where is the part that is not working? And what do you mean by “not working”? What did you expect to happen? What happened instead?


See also:

Sorry if im confusing you.So the part is in the Update method.i commented out .Not working i mean the ball is not locked to the paddle.Here is the full example
void Update()
{
if (hasStarted == false)
{
LockBallToPaddle();
LaunchOnMouseClick();

        hasStarted = true;

    }
        
}

private void LaunchOnMouseClick()
{
    if (Input.GetMouseButtonDown(0))
    {
               GetComponent<Rigidbody2D>().velocity = new Vector2(xPush, yPush);
    }
}

If i use the bool hasStarted = true; in the update method the ball is not locking to the paddle

Where did you declare the hasStarted variable? And what initial value does it have? Have you already tried to add Debug.Logs to your code to see what is going on during runtime?

When you start your game, the value must be false. Otherwise, the if-block does not get executed.

So still not clear my bad.If i swap this on the picture and first i use on the Update method ist not working.
If i swap and put it in the LaunchOnMouseClick() its working.
For me this 2 way of logic seems the same thing but in reality its not.

Forgot to say the challange was to do it by myself.I try and came up with the logic to use the bool in the Update method right after the LaunchOnMouseClick(); because i want the program to execute again the If statement.I failed then i watch Rick how is doing and it seems to me thats the same thing what i tryed.

Thanks you for elaborating on this further. I think I got now what you mean. :slight_smile:

That’s right. They are not the same.

Update gets called each frame. Let’s say we are in frame 1. If hasStarted is false, the code if-block gets executed. Then hasStarted gets set to true. A millisecond later (depending on your framerate), Update gets called again. Since hasStarted is true, the if-block does not get executed anymore.

In Rick’s code, hasStarted gets set to true if the mouse button was clicked and the corresponding if-statement was executed. You can click the mouse button in the first frame or 10 minutes later. As long as the hasStarted was not set to true, the if-block in Update gets executed.

The difference between the two versions is when the variable gets set to true. Apart from that both are working the same way. When you feel it was not working, the code did work but everything happened so fast that you didn’t notice it.

Thank you.Thank you. Of course i can see it now its not the same wooow.How could i not see this on my own.Great help.Sorry for this looong post but i felt i have to understand otherwise im not learning.

No need to apologise. If you have a question, just ask. :slight_smile:

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

Privacy & Terms