Nodachisoft Nodachisoft logo, Katana Sword Icon
  
@Amaji✎ Update January -11-2022

How to get parent and child GameObjects with relative paths in Unity

How to get parent and child GameObjects with relative paths in Unity

Abstract

In Unity 2020.3 LTS version, there are several ways to get a parent and child game object (GameObject) in Unity.

This section provides an example of how to use the "Transform.Find" function, and describes how to go back to the parent to retrieve another child object, as well as how to go back to the parent directory as a relative path, as in Linux and Windows file system.

Access to Children

You can use Transform.Find to get the child object.

When the script attached to Enemy GameObject in hierarchy as shown below, you can get Wolf's Transform using this.gameObject.transform.Find("Monster/Wolf").

Hie1

The sample code is here.

 
EnemyBehaviour.cs
public class EnemyBehaviour : MonoBehaviour{
    void Start() {
        GameObject wolf = this.gameObject.transform.Find("Monster/Wolf").gameObject;
    }
}

Access to parents and access to children of parents other than yourself

If you have the following structure, first get the parent object "Enemy", and then use transform.Find("Monster/Wolf") to get Wolf, using the parent object as a base point.

ViaParent

The sample code is here.

 
EnemyBehaviour.cs
public class EnemyBehaviour : MonoBehaviour{
    void Start() {
        Transform enemyTransform = this.gameObject.transform.parent;
        this.wolf = enemyTransform.Find("Monster/Wolf").gameObject;
    }
}

This can also be written as a path in one shot with Transform.

 
EnemyBehaviour.cs
public class EnemyBehaviour : MonoBehaviour{
    void Start() {
        this.wolf = this.gameObject.transform.Find("../Monster/Wolf").gameObject;
    }
}

This was a memo on how to use Transform. Thanks for reading!

Websites and references that were very helpful.

Page History

date modification
none
 
 
Message sent

Thank you for your message.

Something error has occured!

Sorry. The Error has occurred.We apologize for the inconvenience.Please try again in a few minutes or contact us via DM below.

Twitter:@NodachiSoft_eng
Name:
 
Replay To:
 
Message:
 
Back
Check the content!

Send the following information to us. If you are happy with your submission, please click "Send". If you want to modify it, please click "Back".

Name:
 
Reply To:
 
Message:
 
Enter a confirmation key to make sure that you are not operating from a Robot.
Confirmation Key is 95
Back
 / 
Go to Confirmation
Entry fields
Go to Confirmation

There are 5 articles that may be relevant!

System.Type.GetType returns null in Unity C# code

System.Type.GetType returns null in Unity C# code

#Unity#.net✎ 2022-1-11
System.Type.GetType returns null in Unity C# code
Table Of Contents
How to get parent and child GameObjects with relative paths in Unity
How to get parent and child GameObjects with relative paths in Unity
Abstract
Abstract
Access to Children
Access to Children
Access to parents and access to children of parents other than yourself
Access to parents and access to children of parents other than yourself
Websites and references that were very helpful.
Websites and references that were very helpful.
Page History
Page History
Nodachisoft © 2021