Home > Blockchain >  unity C#- where is the right place to define list
unity C#- where is the right place to define list

Time:04-12

I have a structure in my scene Like this, What I want to get first is all children under Building (test01,test02,test03), then iterate these children to find all sub-children, in this case(Cube01, Cube02. Cube03)

enter image description here

My code is:

List<GameObject> childrenItem = new List<GameObject>();
List<GameObject> firstChildren = new List<GameObject>();
List<GameObject> secondChildren = new List<GameObject>();

private List<GameObject> getChildren(GameObject parentRoot)
{
    childrenItem.Clear();
    for (int i = 0; i < parentRoot.transform.childCount; i  )
    {
        childrenItem.Add(parentRoot.transform.GetChild(i).gameObject);
    }
    return childrenItem;
}

private void OnGUI()
{
    if(GUILayout.Button("Test"))
    {
        firstChildren = getChildren(GameObject.Find("Building"));
        
        for (int i = 0; i < firstChildren.Count; i  )
        {

            secondChildren = getChildren(GameObject.Find("Building"   "/"   firstChildren[i].name));
            Debug.Log(firstChildren.Count   firstChildren[i].name);
            Debug.Log(secondChildren.Count);
        }

    }
}

I think it suppose to output:

3test01 3

3test02 0

3test03 0

but it's actual output:

3Cube01 3

Error NullReferenceException:Object reference not set to an instance of an object

it seem after run this line, the value of secondChildren has been assigned to firstChildren

secondChildren = getChildren(GameObject.Find("Building"   "/"   firstChildren[i].name));

But if I declare the childrenItem inside getChildren function, everything back to wrok. Why this happen? I am a noob wiht C#, if anything I'm missing like variable scope knowledge, please let me know, Thank You Guys!

private List<GameObject> getChildren(GameObject parentRoot)
{
    List<GameObject> childrenItem = new List<GameObject>();
    for (int i = 0; i < parentRoot.transform.childCount; i  )
    {
        childrenItem.Add(parentRoot.transform.GetChild(i).gameObject);
    }
    return childrenItem;
}

CodePudding user response:

getChildren() is returning a reference to the list created and stored as the class variable childrenItem, so each time you call that function it's clearing that same list and filling it again. That reference is then assigned to the firstChildren and secondChildren variables (while the empty lists you originally initialised them with will be discarded and garbage collected assuming no other external references). childrenItem , firstChildren and secondChildren all end up pointing to the exact same list.

When you move the new declaration inside the getChildren() function, it's creating a new list each time that function is called, so it's not overwriting any existing list. Those new lists are then assigned to the firstChildren and secondChildren variables (meaning the new Lists you originally initialised them with become obsolete).

  • Related