How to identify with gameObject from a list of gameObjects was clicked

Hello,

I have an array of randomly generated ball prefabs.


I can check that I am clicking on any ball because the ball prefab, called N-back in the picture above (with many clones as you can see) , has a script attached with this method:

private void OnMouseDown()
    {
        print("clicked");
    }

Yet I want to be able to identify which specific ball I am clicking because eventually I want the user to click them in order trying to remember the order in which the balls were instantiated on the screen, because I instantiate them one by one in random order.
I don’t know how to identify which ball/gameObject/clone of the list created is being clicked by my mouse.
I tried to print the name of each, they all have the same name. The positions are obviously different but I don’t know how this information would be useful to distinguish the balls on click.
Any suggestion would be much appreciated :slight_smile:

Since you’ve already got a script attached to each ball, I suggest you create a variable in that script for storing some kind of unique identifier (e.g. ID number) and set that variable right after you Instantiate the ball.

2 Likes

thank you so much! this worked nicely :smile::smile: