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 /
avatar image
0
Question by Biltekin · Jun 09, 2017 at 05:25 PM · uiarrayvariablearraysscriptingbasics

Variable Vs Array?

There are two method below, which one is faster/better performance? Or have they no difference in performance?

 Button[] MyButtons = new Button[49];
 
 for (int i = 0; i <= 49; i++)
         {
             int Capturediterator = i;
             string str = "Button" + Capturediterator;
             MyButtons [Capturediterator] = GameObject.Find(str).GetComponent<Button>();    
         }


 Button MyButton1,MyButton2,MyButton3..., MyButton50;
 
 MyButton1 =  GameObject.Find("MyButton1").GetComponent<Button>();
 MyButton2 =  GameObject.Find("MyButton2").GetComponent<Button>();
 MyButton3 =  GameObject.Find("MyButton3").GetComponent<Button>();
 and so on...
 MyButton50 =  GameObject.Find("MyButton50").GetComponent<Button>();


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 jmgek · Jun 09, 2017 at 06:24 PM -1
Share

For stuff like this it's better to run tests on your machine, we should try and keep this to unity specific questions (or change the title because you're not working with arrays, you're working with GameObject.find()):

 using System.Diagnostics;
 // ...
 
 Stopwatch sw = new Stopwatch();
 
 sw.Start();
 
 // your array tests
 
 sw.Stop();
 
 Debug.Log("Elapsed={0}",sw.Elapsed);

2 Replies

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

Answer by sleepandpancakes · Jun 09, 2017 at 06:09 PM

Iterating through an array has a negligible impact on performance, at least for these purposes. The first version is clearly better than the second, not for performance reasons, but because the code is MUCH more concise. There's absolutely no reason to hard code each assignment. Remember that you want to repeat yourself as little as possible in your code.

However, neither option is good because GameObject.Find() is frankly a terrible and non-performant way to assign references. That function will search your entire scene for an object with a matching name, and if you're doing that 50 times in a row it will have a significant impact on performance. I would assign the references in the inspector (which lets you sidestep assigning the references in the code altogether). If you need to do it by code then make sure all the buttons are components of one GameObject (or it's children) and use GameObject.GetComponents() or GameObject.GetComponentsInChildren()

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
avatar image
0

Answer by Glurth · Jun 09, 2017 at 06:09 PM

The second is insignificantly faster, but significantly harder to write. More important is how often you will do it: "Find" is slow, so you should try to do this only once, like on start, and SAVE the found buttons for use in say .. Update.

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

7 People are following this question.

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

Related Questions

images assigned to gameobjects by tag 2 Answers

How can I fill an image array with a script 1 Answer

How to load different sprites on a panel based on player level 1 Answer

Getting an error trying to assign a value to an array of classes element 0 Answers

New Gui 4.6 - How to use new GUI for inventory system 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