Search Unity

How do I detect if the player clicks a mouse button?

Discussion in 'Scripting' started by LodestoneNetwork, Jul 13, 2018.

  1. LodestoneNetwork

    LodestoneNetwork

    Joined:
    Jul 12, 2018
    Posts:
    42
    I have tried to make a script that when someone clicks a tree, it should destroy it. But, it doesn't work. The trees aren't on the terrain, but are their own objects. I am trying to detect if it has the "tree" tag, then destroy it.

    Here is my current code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class DestroyTree : MonoBehaviour {
    6.  
    7. // I tried two ways. first way:
    8.     void Start() {
    9.         if(Input.GetButtonDown("Fire1")) {
    10.             Destroy(gameObject);
    11.         }
    12.     }
    13. // second way:
    14.     void onMouseDown() {
    15.         Debug.Log("HI");
    16.         if(gameObject.tag == "Tree") {
    17.             Destroy(gameObject);
    18.         }
    19.     }
    20. }
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    You misspelled OnMouseDown. If you did use that, you don't need to check the tag because you'd put the script only on trees that you'd want to be destroyed when clicked.

    Another option is: https://docs.unity3d.com/ScriptReference/EventSystems.IPointerClickHandler.OnPointerClick.html

    Your first example doesn't make much sense, since the Input would have to be detected only during the first or second frame (in Start()).

    If you wanted this script to be in the scene on just one game object, you could raycast and check if it hits something or not, and then if it does hit, is what it hit a game object tagged 'Tree'.
     
  3. LodestoneNetwork

    LodestoneNetwork

    Joined:
    Jul 12, 2018
    Posts:
    42
    Ok, well it kinda works. I have another problem: I don't want all the trees to die, just the one i clicked. And, is there a way i can have a minimum distance that you can click the tree? (Because i can delete the trees from far away)
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You really don't want to use either method you have described. OnMouseDown is incompatible with the UI system, and will generally cause you problems. Checking for Input.GetKeyDown on every tree is majorly inefficient.

    Instead you need a separate script that handles all of your input. This script can check for the mouse button down, then use a ray cast to detect what is being clicked. Then it can find the appropriate component on each tree and call the appropriate method.

    The better news is that Unity already has this built in. Just put a EventSystem in the scene, add a PhysicsRaycaster to the camera, and add an EventTrigger to each of your trees. In the EventTrigger call Destroy. No code needed.
     
  5. LodestoneNetwork

    LodestoneNetwork

    Joined:
    Jul 12, 2018
    Posts:
    42
    Oh, and one more problem, the trees disappear even when i dont look at them...
     
  6. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    That's because you have this script on every tree. Don't do that. You want to detect input once in one script, then push it out to the correct tree.
     
  7. LodestoneNetwork

    LodestoneNetwork

    Joined:
    Jul 12, 2018
    Posts:
    42
    How do I call "Destroy"? I clicked on OnPointerClicked, then there was no option to destroy anywhere... I am probably just too blind, lol

    BTW, will I be able to create new objects after i destroyed the tree? The point is that the tree drops logs that the player can use.
     
    Last edited: Jul 13, 2018