How to assign a gameObject to a child gameObject?

I’m trying to take a gameobject in a script, and assign it to a gameObject that is childed under a parent.

void SelectPillar()
    {
        GameObject GO = GameObject.Find("Pillars"+pillarEnemy+"(Clone)");
        int GOChildNum = GO.transform.childCount;
        int randomSpawn = Random.Range (0,GOChildNum);
        goodPillar = GO.transform.Find("Pillar"+randomSpawn+"(Clone)");
    }

goodPillar is a gameObject, GO is the parent that has already spawned ingame, and “Pillar”+randomSpawn+“(Clone)” is the child.

I’m getting an error that says I cant assign a gameobject to a transform, but I don’t see how the child is a transform. It should just be a gameObject.

i think the transform.Find returns a transform

Then how am I supposed to access Child GameObjects and manipulate them?

Edit: Nevermind. Apparently you can create a transform of said child, then access the transform.gameobject.

Seems like a very complicated way to access a child. But whatever.

goodPillar = GO.transform.Find("Pillar"+randomSpawn+"(Clone)").gameObject;
1 Like

That works too, thank you.