- Home /
NullReferenceException - When destroying Object
Hello there! I have a problem, i am getting an "NullReferenceException".. And i do not see why, it says its at line 45. Which makes no sense at all.. Everything worked before i added the "Destroy(targetobj);" :-)
Here is my code:
using UnityEngine;
using System.Collections;
public class PickupItem : MonoBehaviour
{
public GameObject player;
public string searchTag = "Item";
public Transform target;
public GameObject targetobj;
public bool item_visible = false;
public float distancemax = 2;
// Use this for initialization
void Start ()
{
player = GameObject.FindGameObjectWithTag ("Player");
}
// Update is called once per frame
void Update ()
{
if (Input.GetButtonDown ("Use")) {
ScanForTarget (false);
item_visible = target.renderer.isVisible;
float distance = Vector3.Distance (player.transform.position, target.transform.position);
Debug.Log ("" + target + " Position: " + target.transform.position + " Player Position: " + player.transform.position + " Distance: " + distance);
if (distance < distancemax && item_visible == true) {
Debug.Log ("'E' PRESSED @Item");
Debug.Log ("targetobj = " + targetobj.ToString ());
Destroy (targetobj);
MyPlayer.MyInventory.Add (new Item ("T-I", 0));
}
}
}
void ScanForTarget (bool debug)
{
target = GetNearestTaggedObject (debug);
targetobj = target.gameObject;
}
Transform GetNearestTaggedObject (bool debugging)
{
float nearestDistanceSqr = Mathf.Infinity;
GameObject[] taggedGameObjects = GameObject.FindGameObjectsWithTag (searchTag);
Transform nearestObj = null;
foreach (GameObject obj in taggedGameObjects) {
Vector3 objectPos = obj.transform.position;
float distanceSqr = (objectPos - transform.position).sqrMagnitude;
if (distanceSqr < nearestDistanceSqr) {
nearestObj = obj.transform;
nearestDistanceSqr = distanceSqr;
}
}
if (debugging == true) {
Debug.Log (nearestObj);
}
return nearestObj;
}
}
Full error message: NullReferenceException: Object reference not set to an instance of an object at ToolTipHandler.Update ()
Thanks!
Frederik
Answer by alexfeature · Dec 28, 2012 at 12:59 PM
Destroy sets reference to null. Hence targetObject is null when you are calling .ToString() on it.
Doing so causes the NullReferenceException.
If you want to log the state of the object do this : Debug.Log(string.Format("targetobj is {0}", targetobj == null ? "destroyed" : "alive"));
Oh! How could i miss that, the Debug was only to check if it took the right GameObject.. Forgot to remove it :-P
Thanks anyway!
It didnt work.. I updated my code and it is still crashing..
Hm. Ok then.
Try adding some more debug lines and see at which point you get the last debug message. Then mark it in code.
It seems that everything is ok in your script. Ofc its hard to tell if any of the other objects are not null. For instance $$anonymous$$yPlayer or $$anonymous$$yInventory? $$anonymous$$aybe they are null?
If i comment out the Destroy(targetobj); it doenst crash? Updated question with full error message.