- Home /
NullReferenceException for reasons I don't understand.
I don't know why, I'm tired and I'm about to rip my god damn head off. Just, just tell me why this is happening to me.
Here are the two codes where I get them.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Controls : MonoBehaviour
{
Vector3 angles;
public static Transform selected;
float rotatospeed;
float anglespeed;
float angle;
GameObject ghost;
Vector3 position, rotato;
public static Vector3 direction;
public static int count;
List<Vector3> positions;
RaycastHit rayhit;
public float speed;
public GameObject prefab;
public static int dosh;
void Start ()
{
rotato = Game.rotato;
count = 0;
positions = new List<Vector3>();
dosh = System.Convert.ToInt32 (Data.XMLread ("requirments", "money"));
}
void Update ()
{
Game.rotato = rotato;
rotato.x += Input.GetAxis ("Vertical") * speed * Time.deltaTime;
rotato.y += Input.GetAxis ("Horizontal") * speed * Time.deltaTime;
rotato.x = Mathf.Clamp (rotato.x, 5, 85);
transform.eulerAngles = rotato;
angle += Input.GetAxis ("Rotate") * 45 * Time.deltaTime;
angle = angle >= 360 || angle <= 0 ? 0 : angle;
direction.y = Mathf.Round (angle * 5) / 5;
direction.z = 90;
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (ghost != null)
{
Destroy (ghost);
}
if (Physics.Raycast (ray, out rayhit))
{
if (Game.mode == "Build")
{
if (rayhit.transform.name == "Ground" && (dosh - 500) >= 0)
{
position = rayhit.point;
position.x = Mathf.Round (position.x * 5) / 5;
position.y = 0;
position.z = Mathf.Round (position.z * 5) / 5;
ghost = Instantiate (prefab, position, Quaternion.Euler (direction)) as GameObject;
if (Input.GetButtonDown ("Fire1"))
{
if (!positions.Contains (position))
{
count += 1;
dosh -= 500;
positions.Add (position);
Instantiate (prefab, position, Quaternion.Euler (direction));
}
}
}
if (Input.GetButtonDown ("Fire2"))
{
if (rayhit.transform.name == "Tree Part") {
//null
Destroy (rayhit.transform.parent.gameObject);
}
if (rayhit.transform.name == "Reflector")
{
count -= 1;
dosh += 500;
Destroy (rayhit.transform.parent.gameObject);
positions.Remove (rayhit.transform.parent.position);
}
}
}
}
if (Game.mode == "Edit")
{
if (selected != null)
{
direction.y = angle;
selected.eulerAngles = direction;
direction.y = Mathf.Round (angle * 5) / 5;
}
if (Input.GetButtonDown ("Fire1") && rayhit.transform.name == "Reflector")
{
selected = rayhit.transform.parent;
angle = selected.eulerAngles.y;
}
}
}
}
and
using UnityEngine;
using System.Collections;
public class Game : MonoBehaviour
{
bool swap;
public static Vector3 rotato;
string click, goals;
public static string mode;
public static bool pause;
public GUISkin skin;
void Start () {
mode = "Build";
}
void Update ()
{
goals = Data.XMLread ("goals", "goalA") + "\n\n" + Data.XMLread ("goals", "goalB")+ "\n\n" + Data.XMLread ("goals", "goalC");
}
void OnGUI () {
GUI.skin = skin;
if (pause == false && Application.loadedLevelName != "Menu")
{
GUI.Label (new Rect (0, 0, 200, 40), "Challenge " + Application.loadedLevelName);
GUI.Label (new Rect (0, 20, 200, 40), "Budget: $" + Data.XMLread ("requirments", "money"));
GUI.Label (new Rect (0, 40, 200, 40), "Budget Remaining: $" + Controls.dosh);
GUI.Label (new Rect (0, Screen.height - 80, 200, 40), mode + " " + "Mode");
if (GUI.Button (new Rect (Screen.width - 600, Screen.height - 40, 200, 20), "Mute"))
{
if (AudioListener.volume == 0)
{
AudioListener.volume = 1;
}
else
{
AudioListener.volume = 0;
}
}
if (GUI.Button (new Rect (Screen.width - 600, Screen.height - 20, 200, 20), "Pause"))
{
pause = !pause;
}
if (GUI.Button (new Rect (Screen.width - 400, Screen.height - 40, 200, 20), " " + mode + " Mode"))
{
if (mode == "Build")
{
mode = "Edit";
}
else if (mode == "Edit")
{
mode = "Build";
}
}
if (GUI.Button (new Rect (Screen.width - 400, Screen.height - 20, 200, 20), "Reset Area"))
{
Application.LoadLevel (Application.loadedLevelName);
}
if (GUI.Button (new Rect (Screen.width - 200, Screen.height - 40, 200, 40), "Finish Layout"))
{
}
if (mode == "Build")
{
GUI.Label (new Rect (0, Screen.height - 60, 200, 40), "Placement Angle: " + Controls.direction.y);
GUI.Label (new Rect (0, Screen.height - 40, 200, 40), "Reflectors Placed: " + Controls.count);
GUI.Label (new Rect (0, Screen.height - 20, 200, 40), "Trees Destroyed: 0");
}
if (mode == "Edit")
{
GUI.Label (new Rect (0, Screen.height - 60, 200, 40), "Selected Object: " + "None");
GUI.Label (new Rect (0, Screen.height - 40, 200, 40), "Angle (Yaw): " + Controls.direction.y);
GUI.Label (new Rect (0, Screen.height - 20, 200, 40), "Position (2D): " + Controls.selected.transform.position.x + ", " + Controls.selected.transform.position.z);
}
if (swap == false)
{
if (GUI.Button (new Rect (Screen.width - 200, 0, 200, 50), "Goals (Click to expand)"))
{
swap = true;
}
}
if (swap == true)
{
if (GUI.Button (new Rect (Screen.width - 200, 0, 200, 300), "(Click to contract)" + "\n\n" + goals))
{
swap = false;
}
}
}
}
}
In the first scripts, it occurs at line 112, where it says
if (Input.GetButtonDown ("Fire1") && rayhit.transform.name == "Reflector")
{
selected = rayhit.transform.parent;
angle = selected.eulerAngles.y;
}
The other one appears in the second script at line 83, where it says
GUI.Label (new Rect (0, Screen.height - 20, 200, 40), "Position (2D): " + Controls.selected.transform.position.x + ", " + Controls.selected.transform.position.z);
I know, it's obvious what the issue is, fixing is and isn't my issue, for the first one just put
if (rayhit.transform.name != null)
Right, well that does not work, at all, it just results in even more exceptions, I don't understand, I keep trying anything, but simple referencing these variables in any way results in a null reference exception in certain situations, I've been trying and trying, researching and researching, just to find out how to solve this.
I just don't get why it wont work.
It's probably something really simple and I'll once again feel like an idiot.
Isn't the problem in the first script related to null rayhit? Did your raycast actually hit anything? Try to debug it, or put simple
Debug.Log(rayhit);
before line 112
And it seems, that if you only set selected in the first script, in line 114, then Controls.selected in second script is null.
Yes, that's exactly the issue and I'm completely aware of that. If I click out into the void I get a NullReferenceException.
Answer by DaveA · Aug 21, 2013 at 07:29 PM
Yeah you need to test up the chain, so to speak. If you have never used the debugger, get some sleep, and tomorrow learn to use the debugger, it will make you 100 times more efficient.
Until then, test
if (rayhit != null && rayhit.transform != null) // make sure these exist before checking the name
Likewise for Controls.selected.transform
You might also use print or Debug.Log statements in there as 'else' or something to print to the console what's going on.
I made it so it checked if the raycast hit anything or not before executing, I should've known because at one point I tested to see if rayhit.transform.name was null and it wasn't, thanks for the help, but can you tell me how to go about using the debugger?
Please take a look at official documentation. And in case of any issues, please post new question.