As an old C# coder i’m used to the class get/set way of doing things… but I’ve noticed most scripts grab a reference using the to access a variable/method … I currently use both ways of doing it, but my question is which is more efficient?
GetComponent isn’t simply reading the variable in a immediate class. It is scanning the GameObject for the component and passing out the reference to it.
well get/set is for defining a property
GetComponent is for retrieving a component of the GameObject that it is attached to.
So… really, we need more detail as to what you mean before we get into efficiency. Otherwise we’re talking about apples and oranges.
Now if you’re talking about the get/sets property for things like the ‘rigidbody’ and ‘animation’ components… well those just wrap a GetComponent call with a property. They’re nearly the same (if only a tiny bit slower due to one extra step on the stack for accessing the property).
I think it depends, if you’re using GetComponent in a Update() loop, some performance will drop. But a single GetComponent call should pose no problem.
I’ve been mixing the two together lately for accessing components.
private Rigidbody m_rigidbody = null;
private Rigidbody rigidbody {
get {
if (!m_rigidbody)
m_rigidbody = GetComponent<Rigidbody>();
return m_rigidbody;
}
}
Example…
GetComponent method
public class ScoreManager {
public int score;
}
public class ScoreSetter {
ScoreManager scoreManager = GetComponent();
scoreManager.score = 2;
}
Get/Set method
public class ScoreManager {
private int score;
public int Score
{
get { return score; }
set { score = value; }
}
public class ScoreSetter {
scoreManager.Score = 2;
}
Both should be identical. Practically every modern compiler, Mono very much included according to a Mono JIT document dated 2006, will perform some degree of optimizations. One of the simplest ones is to inline function commands if they are below a certain size. Property get/set calls are internally treated as function calls.
Your second example is not going to work. Even “as an old c# coder” you’ll first need to somehow get a reference to the ScoreManager class, may it be a static class/method/property or an instance of the class.
GetComponent gives you that reference as long as you inherit from MonoBehavior and got the ScoreManager attached to a gameObject.
Have a look at the first two replies here - it’s really about apples and oranges. GetComponent basically has nothing to do with the property declaration.
Ah yes forgot the static… my bad!
I was just quickly typing out an example and missed it…
OK… so this is a discussion about using STATIC properties vs getting a local reference to the object. Note… you can have static fields as opposed to get/set methods.
This has NOTHING to do with properties vs getcomponent. This has to do with the use of static in general. As a static property fundamentally changes the entire design of the application.
A static property is a SINGLE property in the global space. Where as grabbing a reference to the object (script) and accessing the property from there, can result in multiple similar objects with properties of the same name, but all with different values.
In the static scenario, you’re saying 1 and only ever 1, score will exist, globally.
In the instance scenario, you’re saying each ScoreManager that exists can have a score.