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 Romano · May 12, 2014 at 05:56 AM · updatecomponent

Is enabling and disabling a script component with an Update() function in it a good practice?

Hiya!

I have a player movement script that checks whether or not the player is where the mouse has clicked. The update function in the player movement script looks a bit like this:

 function Update()
 {
    if (player != playerDestinationPosition)
    {
       // then player should move to the destination position
    }
 }

In this game the player won't always be moving around, so I don't want the update to be checking the player's position all the time. My current player script has no update function in it but it enables the player movement script containing the above update function. When the player stops, the player movement script is disabled, meaning the update ceases to run until the player clicks again.

I'm thinking of ditching this method in favour of some coroutines, but can anyone think of any reason why not to use this method?

Thanks very much!

Romano

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by haim96 · May 12, 2014 at 06:05 AM

coroutine are nice but you need to be careful with them. for example not to run corutine more then once when not needed etc... it can impact performance. after all, if all you have inside the update is the movement there is no reason why not to leave it there with simple if statement (you will need it any why with coroutine as well, no? )

Comment
Add comment · Show 8 · 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 Romano · May 12, 2014 at 06:14 AM 0
Share

but if the update is always checking the if statement, isnt that some overhead that could be reduced by disabling the script?

avatar image haim96 · May 12, 2014 at 06:18 AM 0
Share

and you should cache the player transform component so moving it and checking the position will impact less the performance.

 Transform cachedTransform;
 void Start(){
   cachedTransform = GetComponent<Transform>();
 }

then use it like this: cachedTransform.position

avatar image haim96 · May 12, 2014 at 06:25 AM 0
Share

maybe, but you need to remember that you need to activate it back some how by second script. maybe by clicking the mouse button...

any way, here nice optimization doc from unity:

http://docs.unity3d.com/410/Documentation/ScriptReference/index.Performance_Optimization.html

avatar image Romano · May 12, 2014 at 07:17 AM 0
Share

I know about caching the transform, thanks for that :)

I've also read the performance optimisation doc. At the end it says: "3. Use Coroutines. The problem with Update calls is that they happen every frame. Quite possibly checking the distance to the player could be performed only every 5 seconds. This would save a lot of processing power."

Which is the reason I've got a script enabling/ disabling the script with the update function in it.

One script (called player) enables the script-with-the-update ( called player$$anonymous$$oveScript) and player$$anonymous$$oveScript disables itself when the conditions for existing are no longer met.

All seems fine to me, but I haven't seen many people recommend this method which makes me think theres probably a reason for it.

avatar image haim96 · May 12, 2014 at 07:59 AM 1
Share

some good link about this:

https://docs.unity3d.com/Documentation/$$anonymous$$anual/UnderstandingAutomatic$$anonymous$$emory$$anonymous$$anagement.html

and mobile optimization: https://docs.unity3d.com/Documentation/$$anonymous$$anual/iphone-PracticalScriptingOptimizations.html

i prefer to add links to since they explain better thing then i can with my english... :)

and another think about GC.. i read somewhere that it is a good idea to run GC when ever you can - not in game run time, for example when showing pause screen, before level start etc...

Show more comments
avatar image
0

Answer by Owen-Reynolds · May 12, 2014 at 02:45 PM

There's only one player, so not really any reason to complicate things by trying to save a single "am I moving" check each frame.

And Update may eventually want code for managing an idle, or a head look, or a "recoil" after being hit... , where it needs to always run.

Coroutines are also good for things that won't abort, and are the only thing happening that way. If you have a regular move target with Update movement, you can easily change the target, or cancel movement by changing it to your current position. If coroutines move you, tougher. If you aren't careful, they just fight each other.

Comment
Add comment · Show 6 · 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 Romano · May 13, 2014 at 01:18 AM 0
Share

Hiya, thanks for answering :)

It's true that there's only one player but there will be many other scripts with update functions checking just one thing. After a while wouldn't it become helpful to disable some of these, including the player movement?

Currently I'm controlling animation separately so that update function will never have anything other than the movement code.

avatar image Owen-Reynolds · May 13, 2014 at 01:41 PM 0
Share

If you want to go that way, then maybe change every Update into do$$anonymous$$ove? Then the master script in charge of disable/enable can ins$$anonymous$$d keep it's own list of "things that move" and can call or not call do$$anonymous$$ove on each. $$anonymous$$ore of a traditional program$$anonymous$$g style.

I don't know there's anything wrong with disabling a script. But I feel I've gotten funny results, like it still runs OnEnter(?)

avatar image Romano · May 16, 2014 at 04:14 PM 0
Share

I think that's kinda what I'm doing? Unless I haven't understood what you just said, which is possible. I'm not sure what OnEnter is either, interesting that there's funny results with enable/ disable though...

avatar image Owen-Reynolds · May 16, 2014 at 04:57 PM 0
Share

What I mean is, change the name of Update() to do$$anonymous$$ove(). Now it will never auto-run. Your master scene script would have: foreach(mobScript ms in active$$anonymous$$obList) ms.do$$anonymous$$ove();. To "disable" a script, move it to your personal Inactive$$anonymous$$obList.

This is old-style game code, such as XNA would use, or Unity secretly uses. It isn't really better, but it maybe feels like more what you're trying to do.

I'm sure script disable has some precise rules (does OnCollision still get called?,) I've just never tested and feel like I've been surprised.

avatar image Jamora · May 16, 2014 at 05:47 PM 0
Share

the enabled property only affects the functions that are called at set intervals. The $$anonymous$$B documentation lists them all.

Show more comments

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

23 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

Related Questions

Checkibg Rigidbody2d and component problems 0 Answers

How to trigger udpate in editor mode only when I modify component parameters 1 Answer

Turn off a script? 2 Answers

How to Get TextMesh component from the Gameobject the script is attached to? 3 Answers

How to attach a prefab to a script that is added dynamically 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