1
\$\begingroup\$

In top of script i added:

public Transform parentToSearch;

Then:

private void OnGUI()
    {
        if (hasDescription == true && clickForDescription == true)
        {
            foreach (Transform child in parentToSearch)
            {
                if (child.GetComponent<ItemInformation>() != null)
                {
                    ItemInformation iteminformation = child.GetComponent<ItemInformation>();
                    if (child.name == objectHit)
                    {
                        var centeredStyle = GUI.skin.GetStyle("Label");
                        centeredStyle.alignment = TextAnchor.UpperCenter;
                        GUI.Label(new Rect(Screen.width / 2 - 50, Screen.height / 2 - 25, 100, 50), iteminformation.description, centeredStyle);
                    }
                }
            }
        }
    }

But when i make the loop:

foreach (Transform child in parentToSearch)

It will find the first level childs of the first parent but this childs have also childs and so on. And i want to loop all the childs and childs of childs under the Transform.

For example in the Hierarchy i have:

  • Test
    • Test1
    • Test2
    • Test3
      • Test31
      • Test32
    • Test4
    • Test5
      • Test51

So now it will loop only over Test 1,2,3,4,5 but I want also to loop over Test31,Test32,Test51

\$\endgroup\$
1
\$\begingroup\$

You can use a recursive call

void Process(Transform parent, int level = 0)
{
    // TODO: process the parent object.

    foreach (Transform child in parent) {
        // The Process method calls itself. This is the recursive call.
        Process(child, level + 1);
    }
}

You never have to process the child, as each child will become the parent in the recursive call.

I also added a level information allowing you to indent children when printing, adding a label etc. (optional).


If you don't want to process the top-level parent, you can instead process the children.

void Process(Transform parent, int level = 0)
{
    foreach (Transform child in parent) {
        // TODO: process the child object.

        // Process next deeper level
        Process(child, level + 1);
    }
}

Applied to your problem:

private void OnGUI()
{
    if (hasDescription && clickForDescription)
    {
        ProcessOnGUI(parentToSearch);
    }
}

void ProcessOnGUI(Transform parent, int level = 0)
{
    foreach (Transform child in parent) {
        var iteminformation = child.GetComponent<ItemInformation>();
        if (iteminformation != null && child.name == objectHit) {
            var centeredStyle = GUI.skin.GetStyle("Label");
            centeredStyle.alignment = TextAnchor.UpperCenter;
            GUI.Label(new Rect(
                  Screen.width / 2 - 50 + 20 * level, // <== INDENTATION
                  Screen.height / 2 - 25, 100, 50),
                iteminformation.description, centeredStyle);
        }

        // Process next deeper level
        ProcessOnGUI(child, level + 1);
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.