Code effeciency - Advice please

Hi,

Now I’ve got to grips with the basics of coding in UnityScript and had a lot of fun putting a game together, now that I am enjoying myself when coding and getting results, I find myself really keen to start making my UnityScript more efficient. Usually I pretty much have everything running in the Update Function as the game is quite simple. I know this is inefficient as not everything needs to be calculated every frame. So, here are my questions:

  1. MonoBehaviour.InvokeRepeating - Some functions I need to run say once a second straight from the start and all the time. Is this the best way to do it?
  2. Coroutines (Part 1) - For other functions that need to be run once a second but is only used a few times, is a Coroutine with a yield function the best way to do it?
  3. Coroutines (Part 2) - Is calling coroutines from the Update function the best way to call them. I know how to check if a coroutine is still running using a boolean but is there a more efficient way that I am not aware of?
  4. Re-Cycling Scripts - Does this actually have an impact on CPU useage? I know it makes for more efficient coding but would this make the game run faster, especially if there are large number of objects using the same script?
  5. Caching variables - I have heard this is more efficient in cases such as GameObject.Find. So should I create a script that finds the game object once and share it as a static variable with all the other scripts that need to find it? Or should I cache it just locally in the script. Around 200 to 300 objects will be using it if that puts it into perspective.

I am also wondering if I have missed any good video tutorials or general guides WITH EXAMPLES that someone may be aware of. I struggle with just reading theory and need to practice with examples.

Thanks!

Paul

  1. Yes.
  2. Maybe; you can still just use InvokeRepeating, followed by CancelInvoke.
  3. No. Call them from some other coroutine.
  4. No.
  5. Probably.