- Home /
how do you check for a Vector3 == null
hi , I'm pretty new to unity and Im having a few 'problems' converting my javascript code to unitycode. Ive seen that unity can check if a value is null or not (http://unity3d.com/support/documentation/ScriptReference/Object-operator_bool.html) but why can't Vector3 be checked for null with || like all other types ? Is there any short form for this or do I really have to write the if solution?
doesnt work:
var type:String;
var position:Vector3;
...
function (type:String,position:Vector3)
...
this.type = (type||"UNDEFINED");
this.position = (position||Vector3(0,0,0));
...
doesnt work either:
var type:String;
var position:Vector3;
...
function (type:String,position:Vector3)
...
this.type = (type||"UNDEFINED");
this.position = Vector3(0,0,0);
if (position) this.position = position;
...
really as:
var type:String;
var position:Vector3;
...
function (type:String,position:Vector3)
...
this.type = (type||"UNDEFINED");
this.position = Vector3(0,0,0);
if (position==null) this.position = position;
...
Ent
Answer by qJake · Jun 01, 2010 at 09:13 AM
A Vector3 is a struct and therefore can never be null. They start out as Vector3(0, 0, 0)
. If you try to compare a struct to null (an object type), you will get compilation errors.
I think I understand now.
when I define var position:Vector3, it already is Vector3(0,0,0); So the data im reading with my www stream and converting to a vector3 (only if i have all 3 coord.) will then either be 0,0,0 if no correct data is recieved or x,y,z from the data recieved and so the parameter will have to be a Vector3 parameter and can not be null.
actually I got a runtime error with the last method :P
thx.
Just FYI, in C# (not JS), a Vector3 can be made Nullable (see my answer).
Yeah but there's little point in doing that, especially since UnityScript isn't compatible with it. It'd be easier to just make another method header excluding the Vector3 parameter, and dealing with it that way.
Definitely, I'm not saying it's a good idea, :) just possible. And of course mixing C# and UnityScript results in toxic nerve gas, so it's also a bad idea.
Answer by Cyclops · Jun 02, 2010 at 04:17 PM
Since you're using Unityscript (which really isn't Javascript, but Unity's version of it), then this answer doesn't really apply. I'm posting it mainly for information.
However, in C#/.NET/Mono, it is possible to make some types Nullable. For instance, this code works (in C#):
void Start () {
Vector3? foo = null;
//foo = Vector3.zero; // would set non-null, but zeroed.
Vector3 bar = foo.Value; // foo is new type, have to get Value.
if (foo == null)
Debug.Log("foo is null");
else
Debug.Log("foo is non-null");
}
Note the Vector3? with a ?, which is what makes the type nullable.
Of course, if you write some of your scripts in JS, and some in C#, then you run into compilation-order issues, but it would work. Making a type Nullable also adds slightly more syntax, if you assign it to a standard type (you have to access the Value property).
That's not really the correct way to use Nullable. You're not supposed to cast it. You can query HasValue to see if it has a value (or check against null), and then if you need the value call .Value. It'll throw an exception if you try to use .Value when it hasn't been set. $$anonymous$$ore info here: http://msdn.microsoft.com/en-us/library/1t3y8s4s(VS.80).aspx
True. I was just noting that if you did want to assign it to another variable - say transform.position - you'd have to cast it to make it work.
That's what I mean, if you have a nullable Vector3 foo, you can do transform.position = foo.Value. No casting.
Answer by Hfyd2 · Jun 01, 2010 at 09:49 AM
Sorry but it cant be null, just make the co-ordinates 0,0,0 if you dont mind me asking, what were you trying to do?
I think it might be better for him/her to use some other value to figure out whether the object is null... using raw data to ascertain that is kind of overriding its purpose, and can get confusing to maintain.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Vector3 Rotate Around 2 Answers
Help to figure a logic to achieve a movement. 2 Answers
object reflecting of a wall 1 Answer
Problems Scripting a Camera 1 Answer