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 Crystalline · Oct 06, 2015 at 12:09 PM · c#performancefunctions

Calling function found on hundreds objects(best way?)

Hi. I ve been building a script that every 0.1 seconds calls a function on around 250 objects. Game runs fine, but I see 250 cpu calls on the script, so I am wondering if thats fine.

The "main" script looks like this;

     GameObject[] lods;
     float dist;
     Transform player;
     MeshRenderer[] renders;
     int update_time;
 
 
     // Use this for initialization
     void Start () 
     {
         lods = GameObject.FindGameObjectsWithTag("as_lod");
         print( lods.Length );
         InvokeRepeating("lod_Check", 0, 0.1F);
     }
     
     // Update is called once per frame
     void lod_Check () 
     {
         foreach( GameObject ob in lods )
         {
             ob.SendMessage("render_check");
         }
     }

Is there a better way to call functions on multiple objects or is this fine? The other objects have to do a vec3 distance check ,each time they get called.

Thanks for your time.

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
1
Best Answer

Answer by Pharan · Oct 06, 2015 at 01:46 PM

There are many faster ways to do this than SendMessage. Note this old topic: http://answers.unity3d.com/questions/262087/sendmessage-slows-down-unity-dramatically.html

Not to say that SendMessage isn't handy for when you're still starting to implement things. But when it's time to clean up and optimize (or it was already slowing down to begin with), SendMessage should be one of the first things to go.

There was a Unity Tips video about this a while back with Tim Cooper as the speaker: https://www.youtube.com/watch?v=Ozc_hXzp_KU

He recommends that all your instances register to a centralized/singleton script by passing itself (this) to it. It would probably then add it to a List or array. Then just foreach that array.

You already have the lods GameObject array so you probably don't need the self-registry system. Just store a List and find all those components at load time using GetComponent and adding it to the List. Then foreach the List, calling render_check().

btw, you're using unusual naming for your method names. The standard for C# methods is PascalCase. Are you new to Unity?

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 Crystalline · Oct 06, 2015 at 02:10 PM 0
Share

Ok,so the main bad thing would be Send$$anonymous$$essage. I am not new to Unity, I just made up that script quickly to test out things, and I dont care that much about na$$anonymous$$g as long as the script works.

avatar image Dave-Carlile Crystalline · Oct 06, 2015 at 02:17 PM 0
Share

There are also some potential issues with foreach generating garbage due to a bug in the old version of $$anonymous$$ono Unity is using, as well as some other cases. You may be better off iterating using a for loop or manually getting the enumerator.

avatar image Crystalline · Oct 06, 2015 at 02:22 PM 0
Share

Ok, I ve changed the script a bit.

 LODHedge[] Lod_Script;
 
     // Use this for initialization
     void Start () 
     {
         Lod_Script = FindObjectsOfType<LODHedge>();
         InvokeRepeating("UpdateOthers", 0, 0.1F);
     }
     
     // Update is called once per frame
     void UpdateOthers () 
     {
         foreach ( LODHedge script in Lod_Script )
         {
             script.render_check();
         }
     }

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

33 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

space wrap 3D 1 Answer

WorldToScreenPoint in Update/OnGUI: Bad Performance 1 Answer

Generate many (32k) GameObjects approach 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