Checking How Many Times Destroyed of Same Object

I wanna finish game but I m stuck. Here is my script;

public void CastRay()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction, Mathf.Infinity);
       
        if (hit.transform != null)
        {
            if ((hit.transform.gameObject.tag == "A") || (hit.transform.gameObject.tag == "B"))
            {
                Destroy(hit.transform.gameObject);
            }
            else
            {
                gameController1.LessHealth(healthValue); //losing health if click C or D object.
                Destroy(hit.transform.gameObject);
            }          
        }

Now I wanna finish game if “A” object destroyed at least 2 times and “B” object destroyed at least 3 times. All objects respawn forever. My game over script;

public void GameOver()
    {
        gameOverText.text = "Game Over!";
        gameOver = true;
   }

Thanks for all help.

They “Destroy()” method in this case will make the given object disappear from your scene immediately.

What you probably want is a counter that counts how many times you “hit” it, and then when that counter meets or exceeds a certain number (2 in this case), then you want to destroy it.

I wanna count how many times destroy “A” object. Cause “A” object respawn non-stop. Clone of object " A ".

No solution?

You need 2 global variables, to count the hit for A and B.
May be in a class, assigned to the camera with
public static int hitCounterA = 0;
public static int hitCounterB = 0;

1 Like

Thank you @Okto247 I will try.