3 Questions (performance,static variabales,transfrom)

Hello Everybody,

I’ve been diving in to unity for the past month’s and there are a few questions I have about some things I couldn’t find clear answers online, mainly on scripting (maybe even scripting in general). btw I use C#.

1_# What is best performance-wise: Several gameobjects with the same script and individual inspector adjusted variables. And 1 Central script that governs movement of those gameobjects through static variables & limited use of getComponent (script) to acces the specific variables values.

OR

Every gameobject has his own version of the central script, repeating the same code serval times, but without(or less). static variables.

2#
I understand that variables are generally faster, but I’m not sure on the border when it (or if it)becomes a burden on the script. So what if a variable or a specific number, (50 in the example below) has to be repeated 5 times in 5 seperate scripts?
in each script
int moveSpeed = 50

transform.Rotate(0,0,moveSpeed);

OR

transform.Rotate(0,0,50);

OR

One static variable that’s accessed from a seperate script each time it’s needed (and its needed often)

3# Why would you use Transform as a variable instead of specificying the gameObject in a variable and accessing the transform through there (i.e. the_gameobject.transfrom)?

Thanks in advance!

These kind of issues will not really affect the performance of your game in any way. Most games are limited by the rate at which the graphics hardware can display the scene, or the rate at which animation data can be computed. It is possible to write a Unity game that is affected badly by script performance, but that’s probably nothing you need to be concerned about.

For #3, if you access transform via gameObject.transform, it performs GetComponent() behind the scenes, which can be slow if it’s called too often, like in Update or loop. Assigning transform to the variable and using it with that variables skips constant calling of GetComponent.