Search Unity

Targeted addressing of GameObject via ID or index

Discussion in 'Scripting' started by jason57x, Jul 1, 2020.

  1. jason57x

    jason57x

    Joined:
    Feb 21, 2020
    Posts:
    4
    i create 60 objects at runtime, let's call them minions. now i would like to have targeted access to certain minions via an index.
    i also assign an ID to each prefab:

    newplayer.GetComponent<playerID>().ID = i;

    but if i want to access this object in another class i have to do a complete search process:

    foreach (GameObject go in allObjects)//all Minions are checked if
    {

    if (go.GetComponent<PlayerID>().ID == thisID)
    {
    do something...


    but I want to address the object specifically
    for example:

    gameObject(i)

    how can that be?

    thanks a lot
    Ralf
     
  2. MatrixQ

    MatrixQ

    Joined:
    May 16, 2020
    Posts:
    87
    You're going to have to set up an array or a list or something along those lines where you store your minions in, according to their ID. Then you can address them just like you describe.
     
  3. jason57x

    jason57x

    Joined:
    Feb 21, 2020
    Posts:
    4