Many instances of a small script, or reference to objects in one master script?

Hi to all!

Here’s my use case, but the question is more general:

In my scene, I have 100 instances of a sprite. All sprites already hold an instance of SpriteScript (detecting touch). Now, which is better, both performance wise and coding wise (app is for iPad):

  1. To expand SpriteScript with other sprite related functions (i.e. move there, scale, calculate position, etc…)
    Consequence : many instances of a larger script

  2. To send all needed info from sprite to a SpriteController script, which references all sprites? Consequence: keeps the sprite script small, but more coding and more difficult to maintain.

Many thanks!

If a script or class contains a lot of code, it doesn’t matter how many instances you have of that script. The code for a class only exists once. Member variables are eating up memory since they exists on every instance. It might be a little bit faster to handle everything in one script and loop through your instances, but in most cases that’s neglectable.

It’s by far more important to optimise the rendering. Hopefully you already use some kind of sprite-toolkit that batch them into one drawcall.

Unless you have real problems with the performance of code execution, just code logical. In OOP a class is written to serve one specific task.

If you have Unity pro, you can check the cpu usage for all modules in the profiler. Unless you have big hiccups in the code section keep the classes seperated :wink: