Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

OnMouseDown

Discussion in 'Scripting' started by Rastafari, Nov 25, 2013.

  1. Rastafari

    Rastafari

    Joined:
    Jul 18, 2011
    Posts:
    196
    I have a problem regarding the OnMouseDown function.
    When I click the object nothing happends.

    This is my Javascript.
    Code (csharp):
    1. #pragma strict
    2. var player : Transform;
    3. var alight : GameObject;
    4. var isnear : boolean;
    5.  
    6.  
    7.  
    8. function OnMouseDown() {
    9.         if (isnear == true){
    10.         alight.light.enabled =!alight.light.enabled;
    11.         }
    12.         else {
    13.         }
    14.     }
    15.  
    16.     function OnTriggerEnter () {
    17.         isnear = true;
    18.     }
    19.    
    20.         function OnTriggerExit () {
    21.         isnear = false;
    22.     }
     
  2. Patico

    Patico

    Joined:
    May 21, 2013
    Posts:
    886
    Does your object has a Collider? And it has a checked IsTrigger property?
     
  3. Rastafari

    Rastafari

    Joined:
    Jul 18, 2011
    Posts:
    196
    Yes, I get output when I click the button but no result with the lights.
     
  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Could be anything.

    isnear could be false
    alight could be null
    alight.light could be null
    the object this script is attached to could not have a collider
    the object this script is attached to could not have a collider marked as a trigger
    the thing entering the collider attached to this object could not have a rigidbody
    the thing entering the collider attached to this object could not have a collider
     
  5. Rastafari

    Rastafari

    Joined:
    Jul 18, 2011
    Posts:
    196
    isnear could be false - works
    alight could be null - works
    alight.light could be null - works
    the object this script is attached to could not have a collider - works
    the object this script is attached to could not have a collider marked as a trigger - works
    the thing entering the collider attached to this object could not have a rigidbody - not sure if the players needs that
    the thing entering the collider attached to this object could not have a collider - works
     
  6. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    If he doesn't have a rigidbody then the triggers probably aren't firing and isnear is never set to true. Putting some Debug statements at various points in your code would sort it out rather quickly.
     
  7. Rastafari

    Rastafari

    Joined:
    Jul 18, 2011
    Posts:
    196
    The code doesn't seem to register clicks, it does register when the player enters or exits the trigger.





    Code (csharp):
    1. #pragma strict
    2. var player : Transform;
    3. var alight : GameObject;
    4. var isnear : boolean;
    5.  
    6.  
    7.  
    8.  
    9. function OnMouseDown() {
    10.         if (isnear == true){
    11.         alight.light.enabled =!alight.light.enabled;
    12.         Debug.Log("Everything should work");
    13.         }
    14.     Debug.Log("Clicked");
    15.     }
    16.  
    17.     function OnTriggerEnter () {
    18.         isnear = true;
    19.         Debug.Log("Player is near");
    20.     }
    21.    
    22.         function OnTriggerExit () {
    23.         isnear = false;
    24.         Debug.Log("Player isn't near");
    25.     }
     
  8. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    If the camera is inside the bounds of the collider then it won't register clicks because the raycast won't hit it.
     
  9. Rastafari

    Rastafari

    Joined:
    Jul 18, 2011
    Posts:
    196
    So how can I fix it?
     
  10. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Poll Input.GetMouseButtonDown inside an Update method when isnear is true.