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 gayoso · May 14, 2017 at 09:40 PM · scripting problemoptimizationprogrammingreflectionupdate function

Call Update once per object vs Update all in a central script

Hi, I am making a block based game where different blocks have different behaviours and animations. I am handling the animations from within a script which means that each block has a copy of a script that animates it.

I read somewhere that there is some overhead in calling Start and Update because Unity gets the redefined versions I make through reflection. Would it be a significant optimization to make a master script for each type of block, and then have all other scripts register themselves there in a list that the master script goes through on one call to Update, instead of calling the Update function of 100 different scripts?

This way if a level has 200 blocks made up of 4 different types there would be 4 Update calls instead of 200 per frame.

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 toddisarockstar · May 14, 2017 at 10:22 PM 0
Share

I tend to keep as much as possible in a master script in my projects.... but that's just my style. I dont think it would make a noticable difference with proficiency if script is run on an objects or a masterscript. Actually i beleive Unity developers intended smaller scripts to run on objects. Actually there are situations where you $$anonymous$$UST run seperate script. There is no way to detect a Object's collision with out a script on it.

2 Replies

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

Answer by AlwaysSunny · May 14, 2017 at 09:40 PM

This should be pretty easy to test just in case, but I'm confident that your assumption is correct:

Assuming all the work being done is equal, 4 instances / method calls is superior to 200 script instances / calls. Whether the gains are significant is (subjective and) case-specific.

That's not to say you should jump on this paradigm 100% of the time. It's often a question of practicality, your preferred management strategy, and a dozen other considerations.

As your projects / objects evolve, you may encounter unforeseen complications regardless of which direction you take on this issue. In that sense, (and with the stated numbers of objects in your situation) the gains are relatively minimal. Choose the strategy which promotes the cleanest logic. You can refactor later if you must.

Nor are these strategies mutually exclusive. Having a collection of like-objects to iterate over is often a great idea, even if they have scripts with Update methods.

Best,

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 gayoso · May 17, 2017 at 04:10 PM 0
Share

In the end I noticed that the frame drops where only noticeable when there were a lot of a certain block on screen, which has two moving parts each with one script and one Update call. So I decided to first merge these two scripts together which would achieve a similar effect, getting rid of one Update call per block of that kind. I don't know how to view the profiler while running on Android so I don't know the exact improvement, but the slowdown is no longer there! I will definately keep this in $$anonymous$$d if I need to squeeze some extra performance in the future. Thanks a lot for your help!

avatar image
0

Answer by gayoso · May 19, 2017 at 01:53 PM

In the end I noticed that the lag was really only noticeable when there where a lot of a certain block on screen, which has two separate scripts that handle a translation of one part and a rotation of another. I decided that before testing this central script, I would merge these two scripts into one and see the performance increse (which would basically have the same effect of getting rid of one Update call per block of that kind). I don't know how to check the profiler when running on Android, but the slight frame drops I was seeing are definately gone now that I effectively halved the amount of scripts on screen for that block. I wil keep in mind that this optimization worked if I need to squeeze some more performance at some point, but I am happy with the level achieved now. Thanks a lot for your comments, they really helped!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Making the collider change after being instantiate 2 Answers

Regarding Update() Performance 0 Answers

How to Set Animation Position on Slope (animation) help please 1 Answer

Text Update demands too much performance 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