Use of if(Component)
I am currently learning to use a SDK designed for Unity and found some pieces of code that confuse me. The examples for the SDK make heavy use of statements that place a component in an if statement.
Ex.
void Start() {
if(someComponent) {
someComponent.DoSomething();
}
}
As best as I can tell this tests that the component exists/is not null. I would like to know if this is the case and if possible what part of C# is used to do this.
Answer by jgodfrey · Apr 26, 2016 at 10:44 PM
The UnityEngine Object base class (which Component is derived from) has overridden the bool operator for convenience. The details are here:
http://docs.unity3d.com/ScriptReference/Object-operator_Object.html
Your answer
Follow this Question
Related Questions
Unity Update is ignoring the GetKey part of my statement!? 2 Answers
How to use or statements in an if statement. 1 Answer
Add changeable conditions in the editor, similar to unityEvent? 0 Answers
SqrMagnitude intermittently doesn't work for if statement on prefab. (C sharp) 1 Answer
If statement for direction the character is facing 0 Answers