Search Unity

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

Discussion in 'Scripting' started by imposter_syndrom_incarnated, Sep 26, 2020.

  1. imposter_syndrom_incarnated

    imposter_syndrom_incarnated

    Joined:
    May 1, 2020
    Posts:
    51
    Hello,

    I have an array of randomly generated ball prefabs.
    upload_2020-9-26_20-7-59.png
    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:
    Code (CSharp):
    1. private void OnMouseDown()
    2.     {
    3.         print("clicked");
    4.     }
    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 :)
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    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.
     
  3. imposter_syndrom_incarnated

    imposter_syndrom_incarnated

    Joined:
    May 1, 2020
    Posts:
    51
    thank you so much! this worked nicely :D:D