Confused gameobject

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

http://puu.sh/kPv4i/ec3392dfc3.png , http://puu.sh/kPv5f/bbffeec78a.jpg

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

http://puu.sh/kPv0f/f54e3a2ead.png , http://puu.sh/kPv24/da3068bba8.jpg

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.

using

Destroy(colasd.gameObject);

destroys the game object that collided

while using

Destroy(gameObject)

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)
{
hits++;
     if(hits >= maxHits)
    {
         Destroy(otherCollider.gameObject);
    }
}