- Home /
Need a little help with this nullrefexception.
So these pop up if something is missing a reference, but it's sometimes difficult to see what exactly the problem is. So I'll quickly explain my script:
Left clicking on a selectable object (predicated by the public list), will cause selected to be true and return that selectable object's corresponding 'target' gameobject. I'm moving the selectable objects around using target go's for them to follow. The right clicking will move the chosen target to the mouse cursor position.
The nullreference exception that occurs at runtime refers to this line: target.transform.position = hit.point;
public class Movement : MonoBehaviour {
//list of the targets to move
public List<GameObject> targets = new List<GameObject>();
//list of the gameobjects to be selected
public List<GameObject> units = new List<GameObject>();
public bool selected = false;
public Camera camera;
private GameObject target;
void Update () {
Select();
if (selected == true)
{
SetTarget();
}
}
public GameObject Select()
{
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
foreach (GameObject unit in units)
{
if (hit.collider.gameObject == unit)
{
selected = true;
}
else { selected = false; }
}
foreach (GameObject targ in targets)
{
if (hit.collider.gameObject.tag == targ.tag)
{
return targ;
}
}
}
}
return null;
}
public void SetTarget()
{
Debug.Log("selected");
target = Select();
if (Input.GetMouseButtonDown(1))
{
RaycastHit hit;
Ray ray = camera.ScreenPointToRay(Input.mousePosition);
//Raycast
if (Physics.Raycast(ray, out hit))
{
target.transform.position = hit.point;
}
}
}
}
Is the if statement for this:
if (hit.collider.gameObject == unit)
{
selected = true;
}
returning true and therefore exiting? If so, how would I get around this?
you have two returns in Select(). return targ and return null. Since SetTarget is calling it and assigning the result to target it must be null because hit is a struct and therefore cannot be null.
You should check target for null and either return or fix the problem another way.
@hexagonius Hey, so wait why does hit being unable to be null mean that target must be null. And what would cause it to be null?
In your own Select() method you are checking if the mousebutton is down and then checking to see if the raycast hit. If the raycast does not get a hit, you are returning null. Which means that when you run your SetTarget() method and have:
target = Select();
Your object returned in the case of no raycast hit is null. In the case of target being null, when you attempt to set the position of the target to that of the hit point, you are trying to access the transform of a null object and are getting a null reference exception.
Answer by Hanoble · Feb 23, 2017 at 07:45 PM
You need to make sure that the target is not null before assigning the position of it to the hit point. Since Select() can return null, when it does, you will be attempting to access the transform and set the position of a null object. One way to fix this is to do a null check at this line here:
//Raycast and ensure target returned from select was not null
if (Physics.Raycast(ray, out hit) && target != null)
{
target.transform.position = hit.point;
}
This has cleared my nullreference exception but I still don't understand why it keeps being null. I'll select a gameobject within the list and nothing will happen.
I am not sure if you are aware of how to debug unity with Visual Studio (or monodevelop), but if not, here is a good tutorial. This will allow you to actually put breakpoints in your code and stop execution at specific points. It will help you to better understand what is happening when you get null returned.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
3D Platformer Tutorial getting Lerpz's SkinnedMeshRenderer using c# 0 Answers
Object reference not set to an instance of an object... 0 Answers
NullRefenceException error 1 Answer