- Home /
Teleportation Problem
Heres the Script:
var XPos : String = "X";
var ZPos : String = "Z";
var YPos : String = "Y";
function Update(){
transform.gameObject.position.x = XPos;
transform.gameObject.position.z = ZPos;
transform.gameObject.position.y = YPos;
}
function OnGUI(){
XPos = GUI.TextField (Rect (2, 22, 100, 20), XPos, 3);
ZPos = GUI.TextField (Rect (2, 44, 100, 20), ZPos, 3);
YPos = GUI.TextField (Rect (2, 66, 100, 20), YPos, 3);
}
And heres the error:
NullReferenceException: Object reference not set to an instance of an object Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cacheKeyName, System.Type[] cacheKeyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object[] args, System.String cacheKeyName, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.SetProperty (System.Object target, System.String name, System.Object value) Teleportation.Update () (at Assets/WorldBox/Scripts/Teleportation.js:6)
Line 6
Answer by whebert · Apr 09, 2013 at 10:08 PM
Not sure why you're getting a null reference exception, your code shouldn't really even compile. There is no position member of GameObject.
If you need to convert strings to floats, try this:
var x : float = 0;
var y : float = 0;
var z : float = 0;
float.TryParse(XPos, x);
float.TryParse(YPos, y);
float.TryParse(ZPos, z);
transform.position = new Vector3(x,y,z);
it's a nullref because pragma strict was removed from the file, so js is being... "helpful" and assu$$anonymous$$g everything you type after a period is a legitimate member until runtime when it just gracelessly slams headfirst into nullref after nullref as it can't find these things /rant
it's generally a good idea to leave #pragma strict
as the first line of the file.
Ah, cool. I usually code in C#, so hadn't run into that before. Thanks.
ill try this tommorow, in meantime is there a way I can combine my code and your example. I need this for the multiplayer part.
Should just be:
var XPos : String = "X";
var ZPos : String = "Z";
var YPos : String = "Y";
function Update(){
var x : float = 0;
var y : float = 0;
var z : float = 0;
float.TryParse(XPos, x);
float.TryParse(YPos, y);
float.TryParse(ZPos, z);
transform.position = new Vector3(x,y,z);
}
function OnGUI(){
XPos = GUI.TextField (Rect (2, 22, 100, 20), XPos, 3);
ZPos = GUI.TextField (Rect (2, 44, 100, 20), ZPos, 3);
YPos = GUI.TextField (Rect (2, 66, 100, 20), YPos, 3);
}