- Home /
Object Reference Not Set Up As An Instance Of An Object
Full error: NullReferenceException: Object reference not set to an instance of an object PickUpScript.Update () (at Assets/PickUpScript.js:43)
So this script is meant to pick up and drop items when clicked on. When I pick up the item I get that error. Here's the script
#pragma strict
var hitObject : GameObject;
var pickupPosition : Transform;
var hold : boolean = false;
var pickableObject : GameObject[];
function Update () {
if(Input.GetMouseButtonDown(1)) {
var hit : RaycastHit;
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), hit)) {
hitObject = hit.collider.gameObject;
if (hitObject.tag == "pickUp") {
var kinematic : Rigidbody;
kinematic = hitObject.gameObject.GetComponent (Rigidbody);
hitObject.transform.position = pickupPosition.position;
hold = true;
kinematic.isKinematic = false;
}
}
}
if (hold == true) {
hitObject.transform.position = pickupPosition.position;
}
if (Input.GetMouseButtonUp(1)) {
kinematic.isKinematic = true;
hold = false;
}
if (Input.GetMouseButtonUp(0)) {
Debug.Log ("fire");
}
}
How should I change the script so I don't get the error?
Thanks in advance.
(Sorry this is like my 5th question today xD)
So if you hit the button on the mouse but the raycast is false, your kinematic variable will be null, however if you release the mouse button it will try and set it to true for the field/property is$$anonymous$$inematic, you need to work the logic out better.
Answer by Ekta-Mehta-D · Jan 02, 2015 at 12:46 PM
hii..
Update this code and check u still getting the error.
if (hold == true) {
if(hitObject)
{
hitObject.transform.position = pickupPosition.position;
}
}
You might have error in kinematic variable. Declare that variable out of the function. and check condition whether it is null or not.
Yeah I replaced it with that and still got the same error
var kinematic : Rigidbody;
Declare this out of function.
if (Input.Get$$anonymous$$ouseButtonUp(1)) {
if(kinematic)
{
kinematic.is$$anonymous$$inematic = true;
hold = false;
}
}
I mean in the area where u have declared all other variables.
It works without moving the variable but with one problem. When I let go of the object everything goes flying around and into space xD even though it's incredibly funny it's not exactly what I want. Any idea why this might happen?
Your answer
