maxel
maxel

Reputation: 29

a variable counter in OnCollisionEnter2D is counting only one time but the other line is code working fine

click to see the image im trying to increment the variable "res" but somehow it get increment only one time while the destroy() working fine

 private void OnCollisionEnter2D(Collision2D collision)
    {
        if(collision.gameObject.CompareTag("enemy") )
        {
            res++;
            Destroy(gameObject); 
            Debug.Log(res);
        }
    }

need help and thank you .

Upvotes: 1

Views: 58

Answers (1)

Christoph Eckinger
Christoph Eckinger

Reputation: 336

One way to fix it is adding a static in front of the variable for example from

public string name;

to

public static string name;

static turns any variable into a variable that all instances of the class can use and that is the same for all instances. It works as "global" variable.

Upvotes: 1

Related Questions