Search Unity

"On click object disappear" script not working

Discussion in 'Scripting' started by TheSige, Jul 24, 2014.

  1. TheSige

    TheSige

    Joined:
    Mar 22, 2014
    Posts:
    9
    Heya guys,

    So I've made a Java script(Unity script) to make an object disappear after you click with your left mousebutton. But I only want it when you are in a certain area. So I've made the following script:

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. function OnTriggerEnter (other : Collider)
    4.  
    5. {
    6.     if(other.tag == "Player")
    7.  
    8.     {
    9.  
    10.         if(Input.GetMouseButton(0))
    11.  
    12.         {
    13.    
    14.              GetComponent(MeshRenderer).active = false;
    15.  
    16.         }
    17.  
    18.     }
    19. }
    The files below will make it more clear.

    So I've made a collider on the object and attached the script to it.
    I really dont know what's wrong.

    I just want to make the axe (bijl) lying on the table disappear
     

    Attached Files:

  2. rrh

    rrh

    Joined:
    Jul 12, 2012
    Posts:
    331
    What this would do is make the object disappear if you're holding down the mouse button when you enter the trigger. If you enter the trigger first and press the mouse button second, it won't do anything.

    There is an onTriggerStay handler as well that runs every frame while in the trigger:
    http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnTriggerStay.html

    It is perhaps not efficient to re-check the tag every frame, though, so instead you can use the OnTriggerEnter and OnTriggerExit functions to set a bool to true and false, and then in an Update function you can check if the bool is true and the mouse button is down.
     
  3. TheftAdi

    TheftAdi

    Joined:
    Jul 24, 2014
    Posts:
    10
    Code (CSharp):
    1. Void OnMouseDown()
    2. {
    3.      if(Input.GetKeyDown("Dont Now wich Button"){
    4.         gameobject.destroy();
    5. }
    6. }[/]
    7.  
    8. Put this in "OnTriggerStay"
     
  4. TheSige

    TheSige

    Joined:
    Mar 22, 2014
    Posts:
    9
    Thanks very much both of you guys!!
    Worked.

    But yet another question .. how can I make the axe to come in my hand?
    Making a "variable : Transform;"?