0

I have a fairly simple problem (except for me)...I have created an array of card hands, and I want to access their names through an array in the order as appears in the hierchy:

For example, in the hierarchy it shows:

Canvas
   Hand
    card1
    card2
    card3
    card4

I have created this code:

players = GameObject.FindGameObjectsWithTag("Player");
foreach (GameObject go in players)
{
    Debug.Log("Player  " + go + " is named " + go.name);
}

I can access the card hands but the order is wrong. ANy suggestions ?

Thanks

Marlon

1
  • you have to sort it your self Commented Oct 25, 2016 at 5:15

1 Answer 1

2

Never depend on the order of items FindGameObjectsWithTag returns as this is not specified in the documentation and can be unpredictable. You have to add a custom function that loops through the array and finds your specified GameObject by comparing with the GameObject.name property.

GameObject[] players;
void test()
{
    players = GameObject.FindGameObjectsWithTag("Player");
    foreach (GameObject go in players)
    {
        Debug.Log("Player " + go + " is named " + go.name);
    }
}

GameObject getGameObject(string gameObjectName)
{
    for (int i = 0; i < players.Length; i++)
    {
        //Return GameObject if the name Matches
        if (players[i].name == gameObjectName)
        {
            return players[i];
        }
    }

    Debug.Log("No GameObject with the name \"" + gameObjectName + "\" found in the array");
    //No Match found, return null
    return null;
}

Usage:

GameObject card1 = getGameObject("card1");
GameObject card2 = getGameObject("card2");
GameObject card3 = getGameObject("card3");
GameObject card4 = getGameObject("card4");

EDIT:

If your goal is to sort the items in the array, in order, then this should do it:

players = GameObject.FindGameObjectsWithTag("Player");
players = players.OrderBy(c => c.name).ToArray();
Sign up to request clarification or add additional context in comments.

8 Comments

This answer is good however saying that you never should rely on order of items isn't : unity UI (implemented in 4.6) is based on hierarchy order => what's last in hierarchy will be drawn on top. This can be really useful/frustrating depending on the situation ;)
@Kardux I am talking about the FindGameObjectsWithTag function. The order is undefined. Maybe something changed in Unity that I don't know about. What if one of the GameObjects, let's say card3 is disabled before FindGameObjectsWithTag is called? The whole order thing is now messed up. I understand the UI render order which is documented but that has nothing to do with FindGameObjectsWithTag. If you have a link where it says FindGameObjectsWithTag is now working as you
mentioned, please post it.
My bad... I thought you were talking generally and not specifically about FindGameObjectsWithTag method : in this situation relying on hierarchy order would be the worst idea ! (my guess is those methods use IDs that unity gives to the game objects similar to the Instance ID of the components)
Very likely. As for the UI, I think that Unity's UI uses GameObject.transform.GetChild(indexNumber); function to do it's rendering. Not 100p sure about that but that's likely. I modified my answer to add away to sort it if that's what OP is looking for.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.