How do I get the object that the cursor clicked on?

For example, there are 20 objects on the stage and all are different. You can add it to the group, but there are still options?
I want to assign it to a variable when clicking on any object. Is it possible?

You can’t assign an object to a variable. You’d be best to put all the objects into a group, and refer to the group in the collision detection.

If you give each object a unique id (assign it when the object is created), then you can identify the specific group object clicked on.

Сan you show the code how to do this?

Using the group name objectGroup, and each object in that group has an instance variable “Id”, then in Pseudo code:

condition
if mouse over objectGroup
if left mouse button down
trigger once

action
set scene variable selectedObjectId = objectGroup.Variable(Id)

You can then use variable selectedObjectId to select the object using event conditions further in your code.


However, if you just want to do one set of actions when the mouse click occurs on an object, you can avoid using the Id and just use actions using the group name where you’d usually use an object name. So use this type of action instead:

condition
if mouse over objectGroup
if left mouse button down
trigger once

action
Hide objectGroup

or

action
Change opacity of objectGroup to ...


The event conditions create a list of all objects that satisfy the conditions. And when you refence the group in the event actions (or in the events subevents), only those in that list will be used.