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

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? )

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.