how many scripts / update loops do you usually have running? I know from some postings that giving each enemy it’s own script is a bad idea for performance. Since i saw these postings I’m trying to put as much as possible in each script to avoid using more scripts / update()s, but it is getting hard to keep the overview this way.
how costly is a simple if()? Let’s say to check an integer flag. Sometimes I’m just to lazy to make it more elegant and end up using a lot more of them as necessary.
The main problem isn’t so much having multiple scripts that contain ‘ifs’. It’s having scripts that contain an Update() - even if that Update() is empty. Just having it there causes Unity to call it each frame, resulting in greater call overhead. i.e. it’s okay for each enemy to have their own script to handle things - just don’t define an Update() (or LateUpdate(), etc that gets called regularly by Unity).
From what I recall, there is greater Unity->Script overhead (as when Unity calls Update()) than Script-Script overhead (a script function calling another script function). So it’s cheaper to have a single Update() which then distributes tasks to other functions by calling them than for each script to have its own Update().
In other words, instead of each enemy having an Update() that does what needs to be done each frame, define a “DoStuff()” instead, and then have your central Update() loop through each enemy script and call DoStuff(). You get most of the same decentralization benefits, but faster code.
Once you’ve done this, you can further optimize by replacing your central Update() with a coroutine that runs less frequently than every frame for things that only need to be updated, say, every 4 frames or every X seconds. This can be a huge performance gain.
I’m using at the moment for my race demo level this script. I dropped it in the editor under an Empty Game object.
using UnityEngine;
using System.Collections;
public class OnUpdatesLoop : MonoBehaviour
{
private static Button[] button;
private static BaseCarPhysics baseCarPhysics;
private static ChaseCamera chaseCamera;
private static DisplayFPS displayFPS;
private static PlayerInput playerInput;
private void Awake()
{
button = (Button[])FindObjectsOfType(typeof(Button));
baseCarPhysics = (BaseCarPhysics)FindObjectOfType(typeof(BaseCarPhysics));
chaseCamera = (ChaseCamera)FindObjectOfType(typeof(ChaseCamera));
displayFPS = (DisplayFPS)FindObjectOfType(typeof(DisplayFPS));
playerInput = (PlayerInput)FindObjectOfType(typeof(PlayerInput));
AccelerationHelper.OnUpdate();
playerInput.OnAwake();
baseCarPhysics.OnAwake();
}
private void Start()
{
foreach (Button b in button)
{
b.OnStart();
}
playerInput.OnStart();
baseCarPhysics.OnStart();
chaseCamera.OnStart();
}
// Update is called once per frame
private void Update()
{
foreach (Button b in button)
{
b.OnUpdate();
}
playerInput.OnUpdate();
baseCarPhysics.OnUpdate();
displayFPS.OnUpdate();
}
private void LateUpdate()
{
baseCarPhysics.OnLateUpdate();
}
private void FixedUpdate()
{
AccelerationHelper.OnUpdate();
playerInput.OnFixedUpdate();
baseCarPhysics.OnFixedUpdate();
chaseCamera.OnFixedUpdate();
}
}
The great thing about this constructions is for example that you will get control witch script parts should awake first. In all other mono scripts I prefixed the Awake, Start … with OnAwake, OnStart and off-course the same for all other Unity Mono functions.
No
Enums are just glorified number tables with no overhead.
the overhead from Update actually is not function calling itself too, its how unity calls them (its not blabla.Update but a thing thats closer to SendMessage / Reflection which is significantly more costly than a direct call)