Confused gameobject

Problem : my ball disappears when the bricks has " hits >= maxhits " in using this code ,

but it doesn’t have any problems when i use this ,

Colasd is both the name of a member of the class and the parameter of OnCollisionEnter2D. C# (and every language) have a basic rule here, the variable in the “closest” scope is used.

The scope of the colasd parameter is that function only, so it’s the one being destroyed, and I guess that’s the ball colliding with the brick. If you want to destroy the member, use a different for it or the parameter, or write:

Destroy(this.colasd.gameObject); // "this." removes the ambiguity

The compiler should have told you that you’re naming a variable the same way you named another variable of a bigger scope, and that it “hides” the variable of the bigger scope. This is what that message means.



destroys the game object that collided

while using


destroys the game object that the script is attached to.

There is no ambiguity here. You should think of it as this:

void OnCollisionEnter2D(Collision2D otherCollider)
     if(hits >= maxHits)