How to detect click on a game object? (SOLVED)

#1

How can I make a game object click event? I know how to make a GUI click event, but I can’t figure out how to do the same for Game Objects. Here’s an example of what I want to do.

Lets say that I have a game object, and in it, is a box sprite. I also have another game object which is the background. I created a script which creates a click event. What I want is the click event to happen on the box. Here a code example:
image
Now, when I made this script I put it in the box game object. Now, when I run it, It does work. It responds to when I click, but I want it to respond ONLY when I click the box. Because if I click anywhere, it still responds. How do I make it only respond to one spot. How can I do this?
Also, I have found ways to do this online, and it does work, but the problem is that the game I am working on makes the game object move. I found an online script that would create an invisible box around the game object and it respond to only that spot, but when I move the game object around the invisible box doesn’t follow. Is there a way to make the invisible box follow the game object? Here’s the invisible box code I found:
image

0 Likes

#2

If you need to check the click on a rectangular area, then you can use this beautiful and clear post:

You can make this a little easier with the next module if you use sprite:

If you need to track a click on a shape of a complex shape, then there is a wonderful library with support for drag and other interesting things:

And if you need to perfectly check the complex shape, then you can take advantage of my project. But this is almost always not necessary:

2 Likes

#3

thanks for letting me know!

0 Likes