How do you access the children in a parent object

Hello! I am trying to find the best implementation in unity for finding the children of the parent object. I know you can search by doing gameobject.child("lessons").child("container") but If I want to access the child called VRUI-card, I don’t want to write 6 .child to access it. Is there a better way to do this?

7925821--1011967--Capture.PNG

1 Like

Actually it is not a very good idea to acess chidlren by their name. Because if you mistype the name, you won’t notice the error until you run that code fragment. What’s more if "lessons’ is not found, the followup calls to .child will fail, thorwing an exception.

An easy way to quickly find components you need is to attach an unique component to them and then call “GetComponentInChildren” from the parent.

You can also manually assign necessary children to a list through inspector fields.

In your case, I’d create a “VRUi” component or something like that and would assign it to children that should be easy to find.

2 Likes

From memory there’s a pretty good manual page on this, so I’d recommend getting generally familiar with the contents of the manual.

The key points are as follow.
1. The Transform component determines the relationships between GameObjects, such as parenting, and you can access children of a GameObject through its attached transform.

foreach (Transform childTransform in this.transform)
{
   Whatever w = childTransform.GetComponent<Whatever>();
   w.DoThing();
}

2. Where this doesn’t get the job done, you typically want to use GetComponent(s), GetComponent(s)InChildren, or maybe FindObject(s)OfType, all of which you should look up in the Scripting Reference.

3. You can (and should!) store references to GameObjects or specific Components in your own scripts, and either expose them to the Inspector (public, [SerializeField]) or look up values at initialisation time using the points above.

4. You’ve got a full C# environment available to you, so any approach you can use there works here, too. For example, you can have a Singleton-style static instance property if you know there will be exactly one of an object, or you can have a lookup function tailored to your specific use case, or Factory classes which pass along references to objects which need them, and so on. In particular, if you’re thinking about using FindObject(s)OfType then stop and think about your overall structure, first. :wink:

2 Likes

Put a LessonsPanel script on the gameobject that references everything it needs.

What if you went super basic mode and made a public variable that is a game object so you just drag and drop the object?

public GameObject vruiCard

or use a private variable with serialize field

[SerializeField] private GameObject vruiCard

1 Like
  • this thread is over two years old, so your advice is not going to help.
  • dragging and dropping gameobjects is absolute beginner level stuff so the OP likely already knew how to do this
  • this is terrible suggestion if you’re working with a large amount of gameobjects that can’t be made into prefabs for one reason or another
  • doing it this way introduces a bunch of additional pain points since you have to account for what happens if the object isn’t manually assigned
2 Likes