SawyerK
December 29, 2019, 3:12pm
1
I have this script for picking up and throwing objects and i get this error for a couple of lines.
Line: 15, 54, 55, 54 ,57,
NullReferenceException: Object reference not set to an instance of an object
RaycastPickup.Update () (at Assets/Scripts/RaycastPickup.cs:19)
If i remove these lines, i don’t get the errors, but i cant release the object that i grabbed, it stocks with the tempParent and just starts floating away because the gravity not set back to true.
Is there a way to fix this? So i can use the distance and everything.
public class RaycastPickup : MonoBehaviour
{
float throwForce = 300;
RaycastHit hit;
GameObject item;
public GameObject tempParent;
bool isHolding = false;
float distance;
Vector3 objectPos;
void Update()
{
distance = Vector3.Distance(item.transform.position, tempParent.transform.position);
if (distance >= 4f)
{
isHolding = false;
}
if (Input.GetMouseButton(0) && Physics.Raycast(transform.position, transform.forward, out hit, 4) && hit.transform.GetComponent<Rigidbody>())
{
item = hit.transform.gameObject;
if (isHolding == true)
{
isHolding = false;
}
else
if (distance <= 4f)
{
isHolding = true;
item.GetComponent<Rigidbody>().useGravity = false;
}
}
if (isHolding == true)
{
item.GetComponent<Rigidbody>().velocity = Vector3.zero;
item.GetComponent<Rigidbody>().angularVelocity = Vector3.zero;
item.transform.SetParent(tempParent.transform);
if (Input.GetMouseButtonDown(1))
{
item.GetComponent<Rigidbody>().AddForce(tempParent.transform.forward * throwForce);
isHolding = false;
}
}
else
{
objectPos = item.transform.position;
item.transform.SetParent(null);
item.GetComponent<Rigidbody>().useGravity = true;
item.transform.position = objectPos;
}
}
}
on line 15 you are trying to get a distance from a GameObject named “item” but you are not setting this variable anywhere in the code, so unity cant find it.
“Object reference not set to an instance of an object ”
this error means that unity doesn’t find an object on the lines that the error points to
you need to set this variable reference somewhere
SawyerK
December 29, 2019, 4:11pm
3
So line 23 isn’t a reference for this? After i click mb0 the raycast hits the gameobject and that would be the reference.
line 23 happens after the first missing reference
also you should declare “RaycastHit” variable outside the if statement
I haven’t read all of your code, or checked if this work, but it should be something like this
also try reading this or another example from the manual
using UnityEngine;
public class RaycastPickup : MonoBehaviour
{
float throwForce = 300;
RaycastHit hit;
GameObject item;
public GameObject tempParent;
bool isHolding = false;
float distance;
Vector3 objectPos;
void Update()
{
RaycastHit hit;
if (Input.GetMouseButton(0) && Physics.Raycast(transform.position, transform.forward, out hit, 4) && hit.transform.GetComponent<Rigidbody>())
{
item = hit.transform.gameObject;
if (isHolding == true)
{
isHolding = false;
}
else if (distance <= 4f)
{
isHolding = true;
item.GetComponent<Rigidbody>().useGravity = false;
}
}
distance = Vector3.Distance(item.transform.position, tempParent.transform.position);
if (distance >= 4f)
{
isHolding = false;
}
if (isHolding == true)
{
item.GetComponent<Rigidbody>().velocity = Vector3.zero;
item.GetComponent<Rigidbody>().angularVelocity = Vector3.zero;
item.transform.SetParent(tempParent.transform);
if (Input.GetMouseButtonDown(1))
{
item.GetComponent<Rigidbody>().AddForce(tempParent.transform.forward * throwForce);
isHolding = false;
}
}
else
{
objectPos = item.transform.position;
item.transform.SetParent(null);
item.GetComponent<Rigidbody>().useGravity = true;
item.transform.position = objectPos;
}
}
}
SawyerK
December 29, 2019, 4:55pm
5
Thank you! The distance thing works now but it gets me the same error for line 37 which is the distance, interesting.
NullReferenceException: Object reference not set to an instance of an object
RaycastPickup.Update () (at Assets/Scripts/RaycastPickup.cs:38)
are you setting “tempParent” somewhere?
DaDonik
December 29, 2019, 5:05pm
7
Line 21 in the code above does set the ‘item’ only if the condition is true.
In line 37 you are using ‘item’ whether it was assigned, or not.
SawyerK
December 29, 2019, 5:15pm
8
I have an empty gameobject that is the guide that parented to the first person PlayerCamera. It float in front of the player at range 2.5.