NullReferenceException: Object reference not set to an instance of an object
Hi everybody, I'm creating a script that allows an object to "snap" to the position of another object when dropped close enough to it. I have multiple locations that the object can be snapped to, and I created a method to sort through all of the possible snap locations and find which one is closest to the current player position. However, when trying to snap to a particular location, I get an Object Reference error in line 41 of the script which reads transform.position = FindClosestPartner().transform.position;
. Here is the full script.
using UnityEngine;
using System.Collections;
public class DotRearrangement : MonoBehaviour
{
//Initializes all Snapping capabilities
public float closeDist = 3.5f;
public float dist = Mathf.Infinity;
public float moveSpeed;
public float rotationSpeed;
public GameObject FindClosestPartner()
{
GameObject[] partners;
partners = GameObject.FindGameObjectsWithTag("SnapPartner");
GameObject closest = null;
Vector3 myPosition = transform.position;
foreach (GameObject partner in partners)
{
Vector3 diff = partner.transform.position - myPosition;
float curDistance = diff.sqrMagnitude;
if (curDistance < dist)
{
closest = partner;
dist = curDistance;
}
}
return closest;
}
void OnMouseDrag()
{
Vector3 point = Camera.main.ScreenToWorldPoint(Input.mousePosition);
point.z = transform.position.z;
transform.position = point;
}
void OnMouseUp()
{
transform.position = FindClosestPartner().transform.position;
}
}
Any help would be appreciated :)
Answer by HaykAv · Jun 12, 2016 at 02:54 PM
@Arctinium I think you can fix the problem by making your FindClosePartner method return a Vector3 not a GameObject.
public Vector3 FindClosePartner() {
//Your code goes here and then at the end you just return the closest GameObject's position
return closest.transform.position;
}
void OnMouseUp()
{
transform.position = FindClosestPartner();
}
Tell me if this worked or no.
@HaykAv Okay, I tried doing that and rewriting line 41 to look like transform.position = FindClosestPartner();
but it still gives me the error when I run the game every time I release the mouse button. Any ideas? Also thanks for the quick response!
Answer by Mmmpies · Jun 12, 2016 at 04:07 PM
Create a new variable
public Camera myCam;
Drag the main Camera onto it and replace
Vector3 point = Camera.main.ScreenToWorldPoint(Input.mousePosition);
with
Vector3 point = myCam.ScreenToWorldPoint(Input.mousePosition);
Would have been easier if you'd posted the line number but hey you know for next time :)
Hi @$$anonymous$$mmpies , I've done as you said, it is still giving me the same error, but now it's only when the mouse button is released and the player is too far away to snap to a location. When it snaps, no error is given. Would it be because the method is not returning any value when it doesn't detect an object close enough to snap to? The error is at line 30:
using UnityEngine;
using System.Collections;
public class DotRearrangement : $$anonymous$$onoBehaviour
{
//Initializes all Snapping capabilities
public float closeDist = 3.5f;
public float dist = $$anonymous$$athf.Infinity;
public float moveSpeed;
public float rotationSpeed;
public Camera myCam;
public Vector3 FindClosestPartner()
{
GameObject[] partners;
partners = GameObject.FindGameObjectsWithTag("SnapPartner");
GameObject closest = null;
Vector3 myPosition = transform.position;
foreach (GameObject partner in partners)
{
Vector3 diff = partner.transform.position - myPosition;
float curDistance = diff.sqr$$anonymous$$agnitude;
if (curDistance < dist)
{
closest = partner;
dist = curDistance;
}
}
return closest.transform.position; //Error is here
}
void On$$anonymous$$ouseDrag()
{
Vector3 point = myCam.ScreenToWorldPoint(Input.mousePosition);
point.z = transform.position.z;
transform.position = point;
}
void On$$anonymous$$ouseUp()
{
transform.position = FindClosestPartner();
}
}
Which is your return line so it's a different cause even if the error remains the same.
Best guess is how many objects have you actually tagged SnapPartner? I'm betting zero so far as the test scene I created worked but then I'd tagged 2 other objects with that tag.
EDIT - Sorry are you saying it sometimes works? If you set dist to infinity how can it not find something closer or do you try one snap and then try another and dist is still holding the previous value of the closest, which may be less than any available option, if so reset dist to infinity before running the foreach loop.
@$$anonymous$$mmpies I currently have 6 objects tagged SnapPartner. I do not get any errors when the object successfully snaps to a SnapPartner, but when the object unsuccessfully snaps, I get an error. Sorry for being rather unclear with my earlier comment. I tried resetting dist to infinity before the foreach loop, but then it only snaps to one particular SnapPartner, even if it is not the closest.
EDIT: Is there a way to return another value if there is no object close enough to snap to? I think the error is because the if statement doesn't get called because nothing is close enough to snap to, and in that if statement, closest is being defined, and then it is trying to return closest which doesn't exist. Does this make any sense or is it completely unrelated?