Script for each Game Object

Greetings. This question is about optimizing scripts in Unity.

I have 20 characters on stage (or more, whatever).
My task is to make them follow the player’s moving camera with their eyes. There are 2 solutions to this problem.

1.
Create a small script that will take 2 Transform eyes as arguments

The script will handle eye rotation in FixedUpdate (or Update). But this script will be for each character, of which there are 20 and therefore 20 FixedUpdate will be called.

2.
Create a larger script that will process the array of eyes in one FixedUpdate (this is less convenient than using the script for each character separately).

But the question is primarily about how much worse it is to use the first option instead of the second in terms of script performance?

Interesting question, As you’ve stated the second options seems like a more efficient way of handling the logic.
It’s very difficult to give an exact answer to your question because in what unit do we measure the “worse” scripting option…

Performance could also be very dependent on the content of your code.
I think the best & truly only way to answer your question is by conducting an experiment yourself.

Let me know if you have some new insights! :slight_smile:

Yes. I did a test, there is a small difference. I have created 100x100 cubes and each one changes its Y position every frame.

Script per object

One script for all objects

A script:

public class Main : MonoBehaviour
{
    private static Vector2Int m_cubesCount = new Vector2Int(100, 100);
    List<GameObject> cubeList = new List<GameObject>(m_cubesCount.x * m_cubesCount.y);
    GameObject Cubes;

    private bool m_SingleScript = true;

    void Start()
    {
        Cubes = new GameObject("Cubes");

        int i;
        if (m_SingleScript)
        {
            for (i = 0; i < cubeList.Capacity; i++)
            {
                cubeList.Add(GameObject.CreatePrimitive(PrimitiveType.Cube));
                cubeList[i].transform.parent = Cubes.transform;
                cubeList[i].transform.position = new Vector3((int)(i / m_cubesCount.x) * 2.0f, 2.0f, (i % m_cubesCount.y) * 2.0f);
            }
        }
        else
        {
            for (i = 0; i < cubeList.Capacity; i++)
            {
                cubeList.Add(GameObject.CreatePrimitive(PrimitiveType.Cube));
                cubeList[i].transform.parent = Cubes.transform;
                cubeList[i].transform.position = new Vector3((int)(i / m_cubesCount.x) * 2.0f, 2.0f, (i % m_cubesCount.y) * 2.0f);
                cubeList[i].AddComponent<CubeHandler>();
            }
        }
    }

    void Update()
    {
        if (m_SingleScript)
        {
            Vector3 cubeTransform;

            for (int i = 0; i < cubeList.Count; i++)
            {
                cubeTransform = cubeList[i].transform.position;
                cubeTransform.y = 2.0f + Mathf.Sin(cubeTransform.x + Time.fixedTime);

                cubeList[i].transform.position = cubeTransform;
            }
        }
    }
}

And script for game object

public class CubeHandler : MonoBehaviour
{
    void Start()
    {
      
    }

    void Update()
    {
        Vector3 cubeTransform;

        cubeTransform = gameObject.transform.position;
        cubeTransform.y = 2.0f + Mathf.Sin(cubeTransform.x + Time.fixedTime);

        gameObject.transform.position = cubeTransform;
    }
}

Doesn’t seem like much of an improvement. Perhaps you could instead try GPU Instancing? Supposing all of your meshes are the exact same, it would drastically improve performance.

To Improve your performance i would suggest todo so when its needed.

Things to help you improve your performance in terms of CPU:

  • Dynamic Scene Loading (Could improve in case you have got more MonoBehaviours in different locations which are not needed in your current situation)
  • Use a Manager to handle your AI Lifecycle, which means, have an Array (If you dont add/remove) AI out from it and Tick your AI. In case you enable/disable AI runtime, use a list.
  • Depending on the distance of your AI, you can tweak their behaviour, if they are far away from your Player, dont Tick every second but every 15 for example. if they get close, tick every second once.
  • Dont use Update/LateUpdate/FixedUpdate but only from your Manager.
  • Cache values/References

Small Edit: There are more adv. ways ofc. but for 20 AI this should do easly
hope this might help :slight_smile: