- Home /
Is There A Way To Make Sprites Clickable?
Hello I have a quick but important question. Is there any way to make Sprites Clickable(like GUI) through a script without using GUI Texture? I simply want to make my sprite game objects(all they have on them is a sprite renderers) be able to take me to different scenes when I click on them. The reason why I don't want to use GUI Texture is because the sprite gives a more better look than the GUI Texture(you'll have to see it to know what I mean, but I won't show anything). Hope you guys can help me.
Answer by Fornoreason1000 · Oct 05, 2014 at 12:42 AM
Definitely it involves 3 steps
Check if the user has clicked the mouse
Check if the positions of the Mouse and sprite match or within a certain parameter
Run your code
first two are done for you
http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnMouseDown.html
you just need to add a collider to the sprite. (not sure if 2D ones work, doesn't mention them in the docs) then this code.
public class MyGUISprite : MonoBehaviour {
void OnMouseDown()
{
print("clicked")
//do something
}
you can attach this to your sprite object...
Thank you it works. I had to move around the sprite's Z access in order to get the effect I wanted, but it works just fine. Thanks again.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Not working gui buttons 1 Answer
C# Why Won't My GUI Layout Button Appear? 1 Answer
How to dynamically change the text in Unity(Augmented Reality + NYARtoolkit(C#)) ? 0 Answers
Tutorial level 2 Answers