Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Checking How Many Times Destroyed of Same Object

Discussion in 'Scripting' started by gaishinbu, Nov 5, 2015.

  1. gaishinbu

    gaishinbu

    Joined:
    Oct 10, 2015
    Posts:
    64
    I wanna finish game but I m stuck. Here is my script;

    Code (CSharp):
    1.  
    2. public void CastRay()
    3.     {
    4.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    5.         RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction, Mathf.Infinity);
    6.        
    7.         if (hit.transform != null)
    8.         {
    9.             if ((hit.transform.gameObject.tag == "A") || (hit.transform.gameObject.tag == "B"))
    10.             {
    11.                 Destroy(hit.transform.gameObject);
    12.             }
    13.             else
    14.             {
    15.                 gameController1.LessHealth(healthValue); //losing health if click C or D object.
    16.                 Destroy(hit.transform.gameObject);
    17.             }          
    18.         }
    19.  
    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;

    Code (CSharp):
    1.  
    2. public void GameOver()
    3.     {
    4.         gameOverText.text = "Game Over!";
    5.         gameOver = true;
    6.    }
    7.  
    Thanks for all help.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,798
    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.
     
  3. gaishinbu

    gaishinbu

    Joined:
    Oct 10, 2015
    Posts:
    64
    I wanna count how many times destroy "A" object. Cause "A" object respawn non-stop. Clone of object " A ".
     
  4. gaishinbu

    gaishinbu

    Joined:
    Oct 10, 2015
    Posts:
    64
    No solution?
     
  5. Okto247

    Okto247

    Joined:
    Jun 26, 2015
    Posts:
    7
    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;
     
    gaishinbu likes this.
  6. gaishinbu

    gaishinbu

    Joined:
    Oct 10, 2015
    Posts:
    64
    Thank you @Okto247 I will try.