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
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.
Sure enough, it works. The good old trust OnMouseDown to the rescue! Code (csharp): using UnityEngine; using System.Collections; public class ClickVisibility : MonoBehaviour { private bool visible = true; // Use this for initialization void Start () { } // Update is called once per frame void Update () { renderer.enabled = visible; } void OnMouseDown() { visible = !visible; Debug.Log ("toggle " + visible); } }
OnMouse events depend on a collider, nothing else. (Except for GUIText and GUITexture objects.) --Eric
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
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
The objects should be separate; objects that have a rigidbody and children are compound colliders that act as a single object. --Eric
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