• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
9
Question by Azound · Dec 01, 2009 at 04:12 PM · gameobject

How can I get the full path to a GameObject?

I would like to know the full path to a GameObject in the scene. The path would contain all of the parent information for the GameObject.

For example, if I have a scene like this

-A
|-B
  |-C
|-D
-E

then the path for A would be '/A', the path for B would be '/A/B', the path for C would be '/A/B/C', etc.

Clearly we can find any object by its path, but how do I get a path from an object?

Thanks!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
19
Best Answer

Answer by duck · Dec 01, 2009 at 04:30 PM

I couldn't see a built-in function to give you this, so here's one which you can use:

public static string GetGameObjectPath(GameObject obj)
{
    string path = "/" + obj.name;
    while (obj.transform.parent != null)
    {
        obj = obj.transform.parent.gameObject;
        path = "/" + obj.name + path;
    }
    return path;
}

That's C#. Give me a shout if you need it in JS.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image codestage · Feb 16, 2015 at 11:34 AM 4
Share

And without anterior slash:

 private static string GetGameObjectPath(Transform transform)
 {
     string path = transform.name;
     while (transform.parent != null)
     {
         transform = transform.parent;
         path = transform.name + "/" + path;
     }
     return path;
 }
avatar image demented_hedgehog · Mar 09, 2015 at 01:32 AM 0
Share

+1 use of the word anterior. (This is useful code hoewever. I use it for finding duplicate components with a given name).

avatar image winxalex · Mar 31, 2016 at 07:53 AM 0
Share

The idea of getting path is to store it or/and use it for finding the GameObject. ex. GameObject.Find(path)., which won't work if GameObject is hidden and you need to use Resources.FindAllObjectsOfType(GameObject).Where(go=> GetGameObjectPath(go)==path). which won't work for Prefab Instances, cos on the same path- same name there is ghost Prefab object, so you might get the Ghost ins$$anonymous$$d of the instance. As there is not Unity way to check if the GameObject is Prefab or PrefabInstance, during runtime(go vote for that), I was experimenting with Resources.FindAllObjectsOfType(GameObject).Where(go=> GetGameObjectPath(go)==path) && go.transform.hideFlags!=HideFlags.HideInHierachy

avatar image
2

Answer by xtyler · Dec 19, 2018 at 07:16 PM

Concise:

 Debug.Log(string.Join("/", gameObject.GetComponentsInParent<Transform>().Select(t => t.name).Reverse().ToArray()), gameObject);

Remember, if you pass GameObject to Debug.Log, when you can click the log in console it will highlight it in hierarchy

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image kalamtech · Feb 18, 2019 at 02:05 PM 0
Share

I had a problem on the string Join and solved like that: string.Join("/", gameObject.GetComponentsInParent().Reverse().Select(t => t.name).ToArray());

avatar image xtyler · Feb 18, 2019 at 05:30 PM 0
Share

Good catch @kalamtech! Edited to include ToArray()

avatar image
8

Answer by winxalex · Oct 23, 2015 at 09:05 PM

Unity v5 (Editor only)

  if(this.transform!=this.transform.root)
     Debug.Log("Path:"+transform.root.name+"/"+AnimationUtility.CalculateTransformPath (this.transform,transform.root));
 else  you know :)
 

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image josh_s_dev · Mar 31, 2016 at 07:33 AM 0
Share

FYI: This one will only work in the editor due to the use of AnimationUtility which comes from the UnityEditor namespace.

avatar image winxalex josh_s_dev · Mar 31, 2016 at 08:05 AM 0
Share

Question didn't specify runtime or/and editor. I assumed solution of building editor that stores path during edit and use during runtime. Read my reply to duck about problems of runtime use. ;)

avatar image
17

Answer by SukiSan · Jun 03, 2012 at 03:16 PM

Or, for those of us who love the beauty of recursion:

 public static string GetPath(this Transform current) {
     if (current.parent == null)
         return "/" + current.name;
     return current.parent.GetPath() + "/" + current.name;
 }

I implemented it as extension method to be able to simply write transform.GetPath();

For meaningful log messages I added the same for Component:

 public static string GetPath(this Component component) {
     return component.transform.GetPath() + "/" + component.GetType().ToString();
 }

which enables me to write

 Debug.Log("failed to satisfy a condition in " + this.GetPath()"); 
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image frojas_vrmada · May 31, 2021 at 12:04 PM 0
Share

I really like your recursive solution, thank you so much. I think I would rather using component.GetType().Name.

Notice that Transform also inherits from Component and users should be careful when opting for one of both extensions. I am no sure, it depends on efficiency vs usability, but it could be worth it to merge both methods into the Component signature using if(component is Transform transform)

avatar image frojas_vrmada · May 31, 2021 at 12:14 PM 0
Share

$$anonymous$$G:

  public static string GetPathInScene(this Component self) => 
                 self is Transform transform 
                 ? transform.GetPathInScene() 
                 : $"{GetPathInScene(self.transform)}/{self.GetType().Name}";

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Scripts wont work if deactivated then activated? 0 Answers

transform.root.collider From Fps Tutorial issue when organized in view 1 Answer

How to remove objects from a list ? 3 Answers

In Inspector gameobject check is not visible 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges