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
1
Question by alexanderameye · Mar 20, 2017 at 03:05 PM · gameobjectperformancegameobject.findgameobject.findgameobject

Best alternative for GameObject.Find

I have a gameobject in my scene and other gameobjects continuously get added/destroyed as children of that gameobject. Every child that gets added has a unique name like this "Object[index]". And for example if there are 3 children, they will be named "Object[0], Object[1] and Object[2]". I was using GameObject.Find to check whether or not a certain object was already added, and if not, at that object, but people told me that that function was incredibly slow. (I use the GameObject.Find functions inside of OnInspectorGUI in a script added to the parent gameObject). What alternative should I use?

Thanks

Comment
Add comment · Show 1
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 Pengocat · Mar 20, 2017 at 03:30 PM 1
Share

So it is an editor script? You can use Find all you want if it is an editor script since it will not be part of the final game. The best alternative to Find however is to store your references when you add new objects. No need to find something when you know about it already.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by aFeesh · Mar 20, 2017 at 03:23 PM

For your particular situation I would use transform.GetChild(i) which will return the specified child of the parent gameObject. Think of the child objects as members of a list that you can grab using transform.GetChild(i)

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 alexanderameye · Mar 20, 2017 at 03:25 PM 0
Share

That's indeed a function that could be very useful! I'll try it out. Thanks!

avatar image
0

Answer by CasperK · Mar 20, 2017 at 03:24 PM

GameObject.Find performance generally only matters during runtime, since you're using it indside OnInspectorGUI I'm assuming you're writing an EditorScript where it shouldn't matter.

If you still want an alternative you could use a simple manager with a dictionary since all the names are unique,

 Dictionary<string, GameObject> dictonary;

and then use dictonary.ContainsKey to see if it already exists, and if not create it and use dictonary.Add.

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 alexanderameye · Mar 20, 2017 at 03:26 PM 0
Share

So I should really worry about performance in editor scripts?

Thanks for the suggestion! I'm going to try it out!

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

97 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

Related Questions

How do i convert game object counts, into int, and put into string? 0 Answers

How to put ".transform" after "GameObject.FindGameObjectsWithTag()" 1 Answer

How do I find other gameObject and disable it? 2 Answers

How many triangles and verts are too much (for mobile devices)? 0 Answers

Using separate GameObjects for "generic" scripts (Performance) 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