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

Problems with destroying game objects

Discussion in 'Scripting' started by Benzaboy2003, Jul 17, 2017.

  1. Benzaboy2003

    Benzaboy2003

    Joined:
    Jul 17, 2017
    Posts:
    25
    I have made a system to destroy a gameObject (Tree) when it reaches 0 health. There are no errors but when i play it destroys even if im not looking at it? can i have some help?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class TreeHealth : MonoBehaviour {
    6.  
    7.     public int ObjectHealth = 100;
    8.     public int ObjectDamage = 10;
    9.     public GameObject Tree;
    10.    
    11.     void Update () {
    12.         if (ObjectHealth <= 0) {
    13.             Destroy(Tree);
    14.         }
    15.         if (Input.GetMouseButton(0)) {
    16.             ObjectHealth -= ObjectDamage;
    17.         }
    18.     }
    19. }
    20.  
     
  2. cstooch

    cstooch

    Joined:
    Apr 16, 2014
    Posts:
    354
    The reason that is, is because you're just simply checking if the user is holding the mouse button down, and that's about it. No check to whether it's the tree that is being clicked. I assume you want this to happen when your tree is clicked, right? Also, the way you've got it, if the user is holding the mouse button down, the tree's health will very rapidly disappear. You may want to think about forcing the user to click rather than just hold the mouse, but that's up to you.

    Anyways, there are mouse events you can use to do what you want, or you can do what is called ray casting to see if your mouse is over top of your game object when you click. Either way, you'll need a collider on your tree, if you don't have one yet.

    The mouse events are pretty simple though:

    They are listed here as OnMousexxxxxx: https://docs.unity3d.com/ScriptReference/MonoBehaviour.html

    For example, if you create an OnMouseDown() function, you'd move your health reduction piece there. I'm thinking something like this:

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class TreeHealth : MonoBehaviour {
    7.  
    8.     public int ObjectHealth = 100;
    9.     public int ObjectDamage = 10;
    10.     public GameObject Tree;
    11.  
    12.     void Update () {
    13.         if (ObjectHealth <= 0) {
    14.             Destroy(Tree);
    15.         }          
    16.     }
    17.     void OnMouseDown() {
    18.             ObjectHealth -= ObjectDamage;      
    19.     }
    20. }
     
  3. Benzaboy2003

    Benzaboy2003

    Joined:
    Jul 17, 2017
    Posts:
    25
    you were talking about the raycasts, about how it checks if my mouse is over the tree. do you know of a way i can do this without raycasts?
     
  4. Phorsaken

    Phorsaken

    Joined:
    Dec 12, 2013
    Posts:
    27
  5. cstooch

    cstooch

    Joined:
    Apr 16, 2014
    Posts:
    354
    If you're just concerned about not wanting to have to code the raycast, you can use the mouse events I mentioned (OnMouseDown, OnMouseUp, OnMouseDrag). Those do the raycasting for you. But if you're concerned about not wanting raycasts for some other reason (such as performance), @Phorsaken has an idea that would work for that.

    I think the Mouse events would work, unless you're really super concerned about performance, and you've profiled it to be too slow for your needs.
     
  6. Benzaboy2003

    Benzaboy2003

    Joined:
    Jul 17, 2017
    Posts:
    25
    thanks, I copied a raycast script from the unity learning centre and edited it to my own needs.
     
  7. Benzaboy2003

    Benzaboy2003

    Joined:
    Jul 17, 2017
    Posts:
    25