Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
8
Question by Abhiroop-Tandon · Apr 13, 2016 at 01:28 PM · performance optimizationefficiencyfind-gameobject

Which is more efficient GameObject.Find() or public GameObject??

There are two approaches of getting access to a game object in a script: 1) GameObject.Find() 2) public GameObject object (and then drag the game object in the inspector)

Can anyone tell me which is more efficient? I am working on a game and i need to turn of lots of gameObjects to optimize the performance. I know that i can do it both ways but which one will be more efficient performance wise ??

I did find one question but i am looking for a more detailed answer :)

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

3 Replies

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

Answer by Immanuel-Scholz · Apr 13, 2016 at 01:35 PM

Assigning in the inspector is by far more efficient in terms of speed.

GameObject.Find loops through all existing GameObjects and compares their names. Persisted direct links are basically free and initialized when the scene is loaded.

Said that, direct assignments have a couple of draw-backs:

  • You need to remember to assign them, which can become quite a burden depending on your workflow.

  • You can only reference objects that are already present in the editor. So e.g. if you dynamically create objects from script code later, these can not be assigned in the editor (obviously).

  • The pointer use up (very few) memory in the component you need them. This is usually of no real concern, but if you plan to have a LOT of links which you need only once at a blue moon, it might be better to only search them at the time you need them.

  • If you reference any Asset (Texture, Mesh etc) or any prefab that contains an asset, this will be loaded immediately when the scene is loaded. (Of course, GameObject.Find() is not an alternative to direct links here. You need Resources.Load instead and place the assets into Resources/ folder. Just saying.. ;) )

Comment
Add comment · Show 6 · 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 Abhiroop-Tandon · Apr 13, 2016 at 03:14 PM 0
Share

So what should i do in my particular case? I enter a trigger, i close the door behind me and disable all the objects that are behind that door. The script is on the trigger !!

I do agree with your answer and thats somewhat i found and thought previously but how should we find which one is the right thing to do?

Lets say i used the public variable method on my script (which i did), this loads the gameobjects to be disabled when the game begins in the script (which is not used at the moment). On the other hand, if i use Find() on that particular moment then it will loop through every game object finding the right ones to be disabled. So the point is, do i load the gameobjects at the start of the gameplay effeciently (which will be used at some point later in the game) or i load the gameobjects inefficiently when i actually need them ? (i hope i make sense)

avatar image Immanuel-Scholz Abhiroop-Tandon · Apr 14, 2016 at 06:36 AM 1
Share

If you are not sure what method to use, then use any method until you got a reason to do otherwise.

Really, you are not here to philosophize about code, you are here to make games. ;)

If your choosen method has problems, (e.g. for GameObject.Find you experience stutters in gameplay when opening the door. Or for direct assignments you realize that would mean you have to drag'n'drop 3000 objects across 25 scenes to setup everything right).. then just switch to the other method.

If you still can't decide, you should use... dice rolling public field assignment. :P

avatar image Abhiroop-Tandon Immanuel-Scholz · Apr 14, 2016 at 08:37 AM 0
Share

Hahah or toss a coin. If the player cant run a game they should upgrade their machines haha. Thanks for your answer :)

Show more comments
avatar image Shippety · Apr 13, 2018 at 06:24 PM 0
Share

Very helpful, thanks!

avatar image
3

Answer by maaboo · Apr 13, 2016 at 05:06 PM

The more efficient is GameObject.FindWithTag()

Comment
Add comment · Show 1 · 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 Immanuel-Scholz · Apr 14, 2016 at 06:55 AM 1
Share

That is true. Unity caches GameObjects based on their tags, so in a big scene with lots of objects, GameObject.FindWithTag is several orders of magnitudes faster that GameObject.Find.

It still does not beat the simple deserialization of a direct public variable link (in terms of speed).

Just for the record: Object.FindObjectOfType is A LOT slower than GameObject.Find. In our measurements, the relation is roughly: 10x GameObject.FindWithTag = GameObject.Find and 100x GameObject.Find = Object.FindObjectOfType.

Of course, all that only matters if it really matters. 99% of the cases, it just does not matter so if you don't write a tight Update() function in a big scene targeting iPhone 3, go on with any method you like until you run into trouble.

avatar image
0

Answer by SUfIaNAHMAD_ · Jan 03, 2021 at 04:47 AM

GameObject.Find is great when you have a small scene. GameObject.FindWithTag is better for larger scenes with 100s of gameObjects.

public static Object FindObjectOfType(Type type); is useful somewhere, when you have a small scene and you want to find which object has that specific Type. It helps in refactoring other codes.

Comment
Add comment · 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

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

44 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

Related Questions

How do I emulate Minecraft Redstone 0 Answers

(Solved) Findout if an object exist in the scene, but dont trow exception 1 Answer

Less code and avoiding unecessary If-statements 2 Answers

Disable Rigidbody Gravity until collision 2 Answers

FPS drop beacause Dialog with 200 images in it 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