- Home /
NullReferenceException: Object reference not set to an instance of an object JAVA SCRIPT
hello everyone
i am working on a pokemon game and i am basicly following on someone guide on youtube.. any way he gave this code
var startPoint : Vector3;
var endPoint : Vector3;
var speed : float;
private var increment:float;
var isMoving : boolean;
function Start () {
startPoint = transform.position;
endPoint = transform.position;
}
function Update () {
if(increment <=1 && isMoving == true) {
increment += speed/100;
Debug.Log("Moving");
}
else {
isMoving = false;
Debug.Log("Stopped");
}
if(isMoving)
transform.position = Vector3.Lerp(startPoint, endPoint, increment);
if(Input.GetKey("w") && isMoving == false) {
increment = 0;
isMoving = true;
startPoint = transform.position;
endPoint = new Vector3(transform.position.x,transform.position.y,transform.postion.z + 20);
}
}
basicly when you run the game and prees the key W the player should move only he give me this code:
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.GetProperty (System.Object target, System.String name)
UnityScript.Lang.UnityRuntimeServices.GetProperty (System.Object target, System.String name)
anyone knows why?
thanks!
Please use the 101010 Button to format your Code Correctly, makes it much easier to debug and read.
For this and future posts,
Please format your code using 10101 button.
Include the line# of the error
You should tell us at what line is the error, you posted the error but not at what line it occured.
And in my opinion there is nothing in your posted code that can raise up this kind of error, so it may come from another block of code.
You replied with an answer and comment. But its ok Friend, it happens in most first questions.
$$anonymous$$nowing what line would help a lot. If you post the url to the tutorial Ill help compare the code to detect the error.
Answer by Linus · Feb 21, 2014 at 11:16 AM
After more trial and error. The problem was a typo of transform.position.z on the last line.
When double clicking the error in Unity console I was taken to the line where the error was in monoDevelop.
Also make sure the speed is set in the inspector.
[ignore] After some trial of error, it was the Input.GetKey ("w") causing the problem. Its solved by using key code instead. [/ignore]
var startPoint : Vector3;
var endPoint : Vector3;
var speed : float;
private var increment:float;
var isMoving : boolean;
function Start () {
startPoint = transform.position;
endPoint = transform.position;
}
function Update () {
if(increment <=1 && isMoving == true) {
increment += speed/100;
Debug.Log("Moving");
} else {
isMoving = false;
Debug.Log("Stopped");
}
if(isMoving) transform.position = Vector3.Lerp(startPoint, endPoint, increment);
if (Input.GetKey (KeyCode.W) && isMoving == false){
increment = 0;
isMoving = true;
startPoint = transform.position;
endPoint = new Vector3(transform.position.x,transform.position.y,transform.position.z + 20);
}
}
when i am adding { to the code this is the error i get:
BCE0044: expecting }, found ''.
and if i use your code the game runs with no errors but the player still not moving.
any idea why?
Yeah I see it now, $$anonymous$$iss read it. Line 24 and 25 in your question code is suppose to be a one line statement. And not be its own if for the whole section. Dint notice because I stopped using them along time ago as i find it harder to read. Editing my answer
Updated code
still same error when pressing W /:
NullReferenceException: Object reference not set to an instance of an object
Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cache$$anonymous$$eyName, System.Type[] cache$$anonymous$$eyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object[] args, System.String cache$$anonymous$$eyName, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.GetProperty (System.Object target, System.String name)
UnityScript.Lang.UnityRuntimeServices.GetProperty (System.Object target, System.String name)
You will not believe this, transform.postion.z is misspelled on the last line.
That was the whole problem it seems. I now tested it myself. This should be final
Works!... xd yepp it was spelled worng thanks for helping me! have a great day cya
Your answer

Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Destroy GameObject A or B 1 Answer
Cube terrain with perlin noise 1 Answer
What is wrong with this script? 2 Answers