- Home /
The question is answered, right answer was accepted
if syntax for blank variable.
Hi all,
I need to know the syntax for testing if a transform, gameobject, and camera variables have anything in them.
eg.
if (var has contence)
{
do stuff
}
Any help would be appreciated.
Answer by Eric5h5 · Feb 08, 2011 at 05:28 AM
Test whether it's null or not:
if (someVar != null) {stuff}
// or
if (someVar) {stuff}
Answer by Simple · Feb 08, 2011 at 05:05 AM
better to use print() and console or logs, so you will get more info:
var x = ...//gameobject, transform, etc print(x.anyproperty);
like
print(x.name); //for gameObject print(x.position); //for Transform etc
if its empty it will return Null or wrong reference object.
You just need to know properties of different objects.
sorry, I didn't mean in a debug context, I need to test if the object is there so I can do stuff to it if it is.
to avoid this error:
UnassignedReferenceException: The variable ship_cam of Posiition_Controll' has not been assigned.
Answer by Mantiz-GS · Aug 08, 2013 at 03:01 PM
Hi.. maybe you are looking for something like this...
Using variable texture as a public variable, but not asigned in the inspector public Texture2D textura; if (GUI.Button (new Rect (100,100,100,100), texture)) { try { Debug.Log(texture.name); } catch { Debug.Log("There is no texture"); } }
This code get the unassigned error, but I don't know if is the best way to implement a try catch code because use if (texture==null)
do not work and shows the same error. Hope someone have a better answer for this.