LateUpdate and OnCollisionEnter2D. How to check it was end of game

Hey,

I have simple situation. When user push the mouse button my object moves. If this object collides with something player loose the game, when it doesn’t player win the game.

How can I check in the simplest way that player win or loose?
I try to use simple bool flag: isCollide and set it in OnCollisionEnter2D fuction to true.
In LateUpdate function i check this flag:

if( isCollide == true)
// player lost the game
else
  // player win the game

But it doesn’t work because it invokes LateUpdate and my bool flag is false then the collision happened after that.

I think I could wait for example 0.5 second after user push the mouse button and then check it was collision or not, but I don’t think it is the best way to solve my problem.

What do you think about it?

I am a bit confused what your game mechanic is about. :slight_smile:
What’s the timeframe to win? If the object does not collide for x seconds?
And why LateUpdate instead of Update?

I saw LateUpdate invokes after OnCollision: Unity - Manual: Order of execution for event functions

Win scenario:

  • player push the button and object doesn’t collide.

Loose scenario:
-player push the button but object collides with something

I just need to know that OnCollisionEnter2D invoked or not.

EDIT: Sorry. I made a mistake in my first post.
My object moves and after player push the button this object stops.
When it stops I enable collider componet of this object.
If my object collides with something player loose the game, if not - player win.

Ok. Thanks. But why does the game has to end within a single update cycle? Why not just test in Update() and end the game?

It’s not a single update cycle. I just describe my problem in short way.
The problem is how to check it was collide or not, after player push the button, that’s all.

What conditions should I check in Update? My flag isCollide behaves the same in Update and LateUpdate.

Probably I totaly missunderstood your problem, but just check if isCollide = true in Update() and end the game if it is true? :slight_smile:

That’s the point. I can’t do that. I have 2 conditions:

  1. player push the button.
  2. object collide with something( loose scenario) or object doesn’t collide ( win scenario)

I need to check both win and loose scenario:

if( playerPushTheButton == true && isCollide == true)
Debug.log( “Loose secenario”)

else if( playerPushTheButton == true && isCollide == false)
Debug.log( “Win scenario”)

But OnCollisionEnter2D function invoked after that so if object Collide with something I have:

“Win Scenario”, “Object Collide with something”

Alright. I guess I got it. :slight_smile:
You could simply wait one (or a few) Update() cycles bevor checking if this one was a win/fail.

bool playerPushTheButton = false;
bool isCollide = false;
int waitCounter = 0;

void Update(){
    if(playerPushTheButton == true){
        waitCounter++;
        if(isCollide == true){
            //you win
        }
        elseif(waitCounter > 10 && isCollide == false){
            //you lost
        }
    }
    if(Input.Mousebutton(0)){
        playerPushTheButton = true;
    }
}

void OnCollision(){
    isCollide = true
}
1 Like

I made similar thing. I wait 1.0 second after playerPushTheButon then I check isCollide. But your solution is faster I guess. Thanks for your time and help.