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. Dismiss Notice

how to GetMouseButtonDown to react when on specific object

Discussion in 'Scripting' started by GFFG, Jan 19, 2015.

  1. GFFG

    GFFG

    Joined:
    Nov 13, 2014
    Posts:
    224
    I have an animated sprite I want to execute only when I click a specific object, let's call it object X. I've got a script that works, but it works regardless of where I click as long as I click the mouse button anywhere within the game screen.
    How do I specify that it should only execute if I press object X.

    Here's my code :
    Code (CSharp):
    1. public class SpriteActivate1 : MonoBehaviour {
    2.  
    3.     public GameObject SpriteController;
    4.  
    5.     void start () {
    6.         SpriteController.active = false;
    7.  
    8.         }
    9.  
    10.  
    11.  
    12.  
    13.  
    14.  
    15.     // Update is called once per frame
    16.     void Update () {
    17.  
    18.         if (Input.GetMouseButtonDown(0))
    19.  
    20.             SpriteController.active = true;
    21.  
    22.    
    23.  
    24.  
    25.                        
    26.    
    27.     }
    28. }
    29.  
    This script is on object X and allows me to drag and drop my animated sprite onto "SpriteController". But like I said before it doesn't matter what this script is on or where I click, it executes/activates the animated sprite whenever I click the mouse button anywhere. Can anyone tell me how to specify that this script should only work when I click object X? Thanks
     
  2. Shiikarii

    Shiikarii

    Joined:
    Feb 6, 2014
    Posts:
    89
    you can simply check it with the "name" or "tag" or "components"

    Code (CSharp):
    1. //Do this in the FixedUpdate because you are using Physics
    2. void FixedUpdate ()
    3. {
    4.    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    5.    RaycastHit hit;
    6.  
    7.    if (Input.GetMouseButtonDown(0))
    8.    {
    9.       if (Physics.Raycast(ray, out hit, Mathf.Infinity))
    10.       {
    11.          if(hit.gameObject.name == "ball");
    12.  
    13.          if(hit.gameObject.tag == "ball");
    14.  
    15.          if(hit.gameObject.getComponent<BallScript>() != false);
    16.       }
    17.    }
    18. }
     
    GFFG likes this.
  3. GFFG

    GFFG

    Joined:
    Nov 13, 2014
    Posts:
    224
    Okay. I thought it was enough to have a script on a specific object but Ray cast is definitely gonna do what I want.
    Thanks I'll try that.
     
  4. Shiikarii

    Shiikarii

    Joined:
    Feb 6, 2014
    Posts:
    89
    no problem ;)
     
  5. GFFG

    GFFG

    Joined:
    Nov 13, 2014
    Posts:
    224
    I've tried this:
    Code (CSharp):
    1. public class SpriteActivate : MonoBehaviour {
    2.  
    3.     public GameObject SpriteController;
    4.  
    5.  
    6.  
    7.  
    8.  
    9.     void start () {
    10.         SpriteController.active = false;
    11.  
    12.         }
    13.  
    14.  
    15.  
    16.  
    17.  
    18.  
    19.     // Update is called once per frame
    20.     void FixedUpdate ()
    21.     {
    22.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    23.         RaycastHit hit;
    24.        
    25.         if (Input.GetMouseButtonDown(0))
    26.         {
    27.             if (Physics.Raycast(ray, out hit, Mathf.Infinity))
    28.             {
    29.                 if(hit.gameObject.name == "rotator");                  
    30.  
    31.                                 SpriteController.active = true;
    32.  
    33.             }
    34.  
    35.         }
    36.     }
    37.  
    38. }
    39.  

    I apologize in advance if this seems stupid but when I put this script on the object called "rotator", which I called object X before, then it says unity Raycast hit does not have a definition for game object. and in the line "if(hit.gameObject.name == "rotator"); " gameObject turns red. How do I define gameObject as the object that is called "rotator" in this case?
     
  6. Shiikarii

    Shiikarii

    Joined:
    Feb 6, 2014
    Posts:
    89
    You need to Drag this Script onto you Camera, because the Raycast uses the OnScreenMousePosition.
    Also check if your "rotator" Object has got an Collider (do not Trigger that Collider)

    when you have finished that change the Script like that:
    Code (CSharp):
    1. if(hit.gameObject.name == "rotator")
    2. {
    3.    hit.gameObject.getComponent<SpriteController>().active = true;
    4. }
     
  7. GFFG

    GFFG

    Joined:
    Nov 13, 2014
    Posts:
    224
    Alright, I dragged this script onto the camera and the "rotator" Object has a Collider that isn't set as trigger (Mesh Collider). And I changed the last part to what you wrote but it still says gameObject is not defined and its red. Do i have to write something about the SpriteController object before void Start? I really appreciate your help Im new to this and trying to learn so thanks a lot for your help ;-)
     
  8. Shiikarii

    Shiikarii

    Joined:
    Feb 6, 2014
    Posts:
    89
    better you make an image that i can see whats your error :)

    also make shure your gameobject is named "rotator" !

    the next thing is...SpriteController... is that a script you have written.. if it is a costum script from you attach that to your rotator Gameobject, next you could look is (aktive) is that a variable in your spritecontroller if it is make shure you have set the variable to public

    if you mean (aktive)to enable or diable your script write that:

    hit.gameobject.getcomponent<SpriteController>().enabled = true;
     
  9. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    Just use OnMouseDown. No need for manually raycasting and so on. (Also, using GetMouseButtonDown in FixedUpdate does not work and should never be used, since *Down and *Up events are only true for the single frame in which they occur, which may or may not coincide with a physics frame.)

    --Eric
     
    GFFG and Kiwasi like this.
  10. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Perfect. Except you should use IPointerClickHandler and a physics raycaster. Slightly more complex, but ultimately more powerful.
     
  11. GFFG

    GFFG

    Joined:
    Nov 13, 2014
    Posts:
    224
    So simple yet brilliant. This worked and was quick and easy, Thank you so much!! :)
     
  12. GFFG

    GFFG

    Joined:
    Nov 13, 2014
    Posts:
    224
    Thanks everybody for your input, the result I was trying to achieve was achieved with this script :
    Code (CSharp):
    1. public class SpriteActivate1 : MonoBehaviour {
    2.  
    3.     public GameObject SpriteController;
    4.  
    5.     void start () {
    6.         SpriteController.active = false;
    7.  
    8.         }
    9.  
    10.  
    11.  
    12.  
    13.  
    14.  
    15.    
    16.     void OnMouseDown () {
    17.  
    18.  
    19.  
    20.             SpriteController.active = true;
    21.  
    22.    
    23.  
    24.  
    25.                        
    26.    
    27.     }
    28. }
    29.  
    I'm so happy now thank you all for your time and help!!! ;-)
     
  13. GFFG

    GFFG

    Joined:
    Nov 13, 2014
    Posts:
    224

    I got the fix. I know i must have confused you with "SpriteController" calling it an object before. SpiteController was just a public float to be able to drag and drop my animated sprite onto the "rotator" in the inspector. Thanks for your help though I've learned a little more about Raycasting ;-) #LongWayToGoThough