Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 ThePwnicorn · Mar 14, 2014 at 11:21 PM · multipleforeachtimes

For each (foreach) statement running hundreds of times.

I have a bit of code that respawns a character after death. It's still a work in progress, but is mostly functioning how I want. Instead of destroying the object I was hoping it would be more efficient to disable it and move it to a predefined point, then re-enable it. I have created a coroutine to handle this (the re-enabling part)which places all the gameobject's colliders and renderers into arrays then uses a foreach statement to re-enable them. There's a huge fps drop when this happens. I placed a debug.log line in the loop to see how often it was being called and it's anywhere from 50 - 500 times!

My understanding was that each instruction is executed once per item in the array (there are 8 renderers and 1 box collider). Renderers foreach is called 424 times (53 x 8 renderers) and colliders foreach is called 53 (53 x 1 box collider). Shouldn't it just run 8 times and 1 time?!

 IEnumerator Recreate()
     {
         yield return new WaitForSeconds(1);
         
         gameObject.transform.position = SpawnLoc;
         
         
         Component[] renderers = gameObject.GetComponentsInChildren<SkinnedMeshRenderer>();
         Component[] colliders = gameObject.GetComponentsInChildren<BoxCollider>();
         foreach(SkinnedMeshRenderer item in renderers)
         {
             item.enabled = true;
             Debug.Log ("skinned mesh renderers being turned on");
             Debug.Log ( renderers.Length);
         }
         health = maxHealth;
         mana = maxMana;
         GetComponent<LifeHUD>().justRespawned = true;
 
 
         isAlive = true;
         yield return new WaitForSeconds(3);
 
         foreach(BoxCollider item in colliders)
         {
             item.enabled = true;
             Debug.Log ("BoxColliders being turned on");
         }
 
 
     }
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
0
Best Answer

Answer by Eric5h5 · Mar 14, 2014 at 11:29 PM

You're calling the function repeatedly elsewhere, probably Update, so you have many instances running.

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 ThePwnicorn · Mar 15, 2014 at 12:21 AM 0
Share

Oh, so much better! I was needlessly running this when a boolean was switched to false, then was setting it to true within the coroutine (after a waitforseconds). Dumb.

Thanks Eric, and a HUGE thanks for being an unknowing code tutor! You have answered so many questions on these "forums" that I've been stumped by and learned from!

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

21 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

Related Questions

Rendering an object multiple times 3 Answers

Problems with multiple layers. 1 Answer

Cloning Problem Script 0 Answers

Code executing multiple times? 1 Answer

Multiple Animations for the Same GameObject 2 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