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

OnMouseDown on Invisible Sprite?

Discussion in '2D' started by imgodot, Dec 28, 2013.

  1. imgodot

    imgodot

    Joined:
    Nov 29, 2013
    Posts:
    212
    Hello all,

    I have a sprite that I've made invisible by turning the spriterenderer off.
    When I click on the sprite I want it to become visible.
    OnMouseDown() does not seem to be triggered on my "invisible" sprite, which seems perfectly reasonable to me.

    If I want to be able to use OnMouseDown() on an invisible sprite, how can I do that?
    (I.e. how can I make a sprite invisible, but clickable AND made visible when clicked)?

    Thanks.

    -- Paul
     
  2. unitylover

    unitylover

    Joined:
    Jul 6, 2013
    Posts:
    346
    You should still be able to catch mouse down events from an invisible sprite if you have a collider on it. Will do some tests and report back.
     
  3. unitylover

    unitylover

    Joined:
    Jul 6, 2013
    Posts:
    346
    Sure enough, it works. The good old trust OnMouseDown to the rescue!

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class ClickVisibility : MonoBehaviour {
    6.  
    7.     private bool visible = true;
    8.  
    9.     // Use this for initialization
    10.     void Start () {
    11.    
    12.     }
    13.    
    14.     // Update is called once per frame
    15.     void Update () {
    16.         renderer.enabled = visible;
    17.     }
    18.  
    19.     void OnMouseDown() {
    20.         visible = !visible;
    21.         Debug.Log ("toggle " + visible);
    22.     }
    23. }
    24.  
    25.  
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,395
    OnMouse events depend on a collider, nothing else. (Except for GUIText and GUITexture objects.)

    --Eric
     
  5. imgodot

    imgodot

    Joined:
    Nov 29, 2013
    Posts:
    212
    I created a script with an OnMouseDown event and an Update() event that is attached to a sprite with a 2D circle collider on it. The Update is firing but the OnMouseDown is not.

    I have a quad with a texture and a mesh collider that is positioned BEHIND my sprite and, if I remove the quad from the scene or disable its mesh collider, I get the mouse events in the sprite.

    What do I need to do to prevent my background quad from "swallowing" the mouse events?
    I could set it to "ignoreraycast" but I can't do that.

    -- Paul
     
    Last edited: Dec 29, 2013
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,395
    I'd suggest getting rid of the mesh collider and use a polygon collider instead.

    --Eric
     
  7. imgodot

    imgodot

    Joined:
    Nov 29, 2013
    Posts:
    212
    Eric,

    Thanks, but it still won't work for my situation.

    In my situation I have two sprites, one is a child of the other.
    The child sprite has the script with the OnMouseDown() event that never fires.

    If I make the child a standalone sprite with the script it works, but that's not what I want.
    If I put the script with the OnMouseDown() event on the parent sprite it works from the parent script, but that's not where I want it.

    NOTE:
    The parent has a rigidbody2D and a polygon collider.
    The child sprite has a circle collider.

    Any ideas?

    -- Paul
     
  8. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,395
    The objects should be separate; objects that have a rigidbody and children are compound colliders that act as a single object.

    --Eric
     
  9. imgodot

    imgodot

    Joined:
    Nov 29, 2013
    Posts:
    212
    Eric,

    Got it. That clarifies the situation.
    It works as you say.

    For my own edification, this only applies to the exact situation you stated; a parent with a rigidbody and child object with colliders?
    I.e. a parent sprite with a collider and a child sprite with a collider (no rigidbody anywhere) would not exhibit this compound collider behaviour?

    -- Paul
     
  10. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,395
    Yep.

    --Eric
     
  11. imgodot

    imgodot

    Joined:
    Nov 29, 2013
    Posts:
    212
    Great. Got it.

    Thanks Eric!