- Home /
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!
Answer by Graham-Dunnett · Oct 05, 2012 at 08:07 PM
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.
So there's no performance backdraw in communicating between scripts and variable assignment at all?
Yes, there are costs. rudidev08 has given some good advice for #3. What I was trying to say is that these concerns are not likely to affect your final game. I doubt you'd be able to measure any performance difference for your #2, so you might want to design your code to make it easy to read and manage, rather than worry about how the compilers convert your code into byte code. So for #1, I'd have a single script and use the inspectors to adjust values for each game object. That would be easier to manage rather than having N variations of the same script.
Answer by rudidev08 · Oct 05, 2012 at 08:43 PM
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.
Your answer
Follow this Question
Related Questions
Find GameObject Position(x,z) and rotation(y) 1 Answer
Calling gameobject.transform vs. just calling transform directly - Performance negligible? 1 Answer
Setting positions of cubes directly without using transform 1 Answer
Are .gameObject and .transform both using GetComponent() in the background? 1 Answer
Does moving a charactercontroller via it's transform hurt performance? 1 Answer