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

Destroy Object and scoring points

Discussion in 'Scripting' started by Splattex, Mar 20, 2015.

  1. Splattex

    Splattex

    Joined:
    Mar 19, 2015
    Posts:
    2
    Hi,
    i'm italian, so excuse me for my english :)

    I have a problem with 2 scripts, so... this my void Update Enemy script:

    void Update ()
    {
    if (health <= 0)
    {
    Destroy (gameObject);
    }

    }


    when the enemy destroy, i want a counter, for the points.
    So, i have the script Shoot in the Player, with a variable
    public int contatore;
    and with this code:

    void Start () {
    Screen.showCursor = false;
    contatore = 0;
    }

    void Update () {
    if (Input.GetMouseButtonDown(0)) {
    audio.PlayOneShot (suonoSparo);
    ShootFunction();
    }
    }

    So... in the Shoot Script, in the Update function... how can I increase the variable contatore when i destroy the Game Object in the Enemy Script??


    Thank you very much.
     

    Attached Files:

    • Enemy.cs
      File size:
      425 bytes
      Views:
      630
    • Shoot.cs
      File size:
      1.3 KB
      Views:
      726
  2. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,548
    There's several ways to do this. One way would be by finding the GameObject with the ShootScript attached to it :
    Code (CSharp):
    1. //EnemyScript.cs
    2.  
    3. bool dead;
    4.  
    5. void Update(){
    6. if(health <=0  && !dead){
    7.  
    8. dead  = true;
    9.  
    10. //Find the gameObject with the ShootScript
    11. ShootScript shooter = GameObject.FindObjectOfType(typeof(ShootScript)) as ShootScript;
    12.  
    13. //Inrease contator by 1
    14. shooter.contatore ++;
    15.  
    16. //Destroy the gameObject
    17. Destroy(gameObject);
    18. }
    19. }
     
  3. Splattex

    Splattex

    Joined:
    Mar 19, 2015
    Posts:
    2
    OOOHHH YESSSS!
    It's perfect! Thank youuu