Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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
2
Question by valeriopavan · Feb 01, 2019 at 09:23 AM · performancegameobject.find

The better (and the less expensive) way to find a GameObject?

Hello guys, I have a great perplexity, and I hope someone can give me some advice :)

In term of performance, what is the less ‘expensive' way in order to find a gameobject?

1) Public GameObject myGO;

2) GameObject myGO = this.transform.parent.GetChild(0).GetChild(0).gameObject;

3) GameObject myGO = GameObject.Find("myObject")

Thanks a lot! :)

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

1 Reply

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

Answer by tormentoarmagedoom · Feb 01, 2019 at 09:31 AM

Good day.

1- For performance, best option is to assign the object via inspector (public variable)


2- But this can be almost impossible for so many cases, then, 2nd best option is to find it via transform/childs from an object already known (this parent defined via inspector public variable) https://docs.unity3d.com/ScriptReference/Transform.Find.html


3- If you need to find it from "0", no other references, then best option is using https://docs.unity3d.com/ScriptReference/GameObject.FindWithTag.html

You can also find it (most used option i think) by finding all the objects with a speecific tag with a foreach loop to find all of them (you get an array) https://docs.unity3d.com/ScriptReference/GameObject.FindGameObjectsWithTag.html and then use an if (name == something) or if (position) or something to select the exact object from that array


4- And last way (less performance, more CPU needed) is using https://docs.unity3d.com/ScriptReference/GameObject.Find.html


Bye!

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 valeriopavan · Feb 01, 2019 at 09:34 AM 1
Share

Thanks a Lot @tormentoarmagedoom! Have a nice day! :)

avatar image Bunny83 · Feb 01, 2019 at 11:09 AM 4
Share

Actually his second example would be in between your first and second point. Using GetChild is most likely faster than using Transform.Find. However unless you can guarantee the structure of your hierarchy i would always use Transform.Find if possible. $$anonymous$$eep in $$anonymous$$d that you can use a "path" structure in a single Transform.Find call:

 GameObject myGO = transform.parent.Find("Child1/NestedChild").gameObject;

This works as long as the names of the gameobjects don't change. Relying on the child order is never a good idea. Also keep in $$anonymous$$d to cache such references. So you wouldn't / shouldn't execute this very often, so the performance becomes quite irrelevant. It's more important to have robust code that doesn't break when you add another child to one of those objects.

avatar image valeriopavan Bunny83 · Feb 01, 2019 at 12:19 PM 1
Share

Thank You! @Bunny83

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.

Follow this Question

Answers Answers and Comments

116 People are following this question.

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

Related Questions

C# Invoke vs. Unity SendMessage 2 Answers

Does a coroutine do all its work during one frame? 1 Answer

Problem with Unity 4 and Nvidia GTX 600 series video cards 0 Answers

character view range 2 Answers

Profiler Windows - What is "Others" 1 Answer


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