NullReferenceException Error :(

So im tryin to learn c# only been learning for a few days, i been following a tutorial to make a strategy game and everything has been going fine. Today i implimented a pathfinding system and that seems to be working. But it seems to have broken my mousecontrol script. The game runs but when i click or drag i get an error:

If i move the units off screen space, then i can drag and click as much as i want.

Here is the script and i know i make school boy error by not taking earlia copy.

using UnityEngine;
using System.Collections;

public class Mouse : MonoBehaviour 
{
	#region Class Variables

	RaycastHit hit;


	public static Vector3 RightClickPoint;
	public static ArrayList CurrentlySelectedUnits = new ArrayList(); // of gameGameObject
	public static ArrayList UnitsOnScreen = new ArrayList (); // of Gameobject
	public static ArrayList UnitsInDrag = new ArrayList(); // of gameobject

	private bool FinishDragOnThisFrame;
	private bool StartedDrag;
	public GUIStyle MouseDragSkin;
	public GameObject Target;
	private static Vector3 mouseDownPoint;
	private static Vector3 currentMousePoint; // In World Space
	public static bool UserIsDragging;
	private static float TimeLimitBeforeDeclareDrag = 1.0f;
	private static float TimeLeftBeforeDeclareDrag;
	private static Vector2 MouseDragStart;
	private static float clickDragZone = 1.3f;

	//GUI

	private float boxWidth;
	private float boxHeight;
	private float boxTop;
	private float boxLeft;
	private static Vector2 boxStart;
	private static Vector2 boxFinish;

	#endregion

	void Update() 
	{
		Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
			
		if (Physics.Raycast (ray, out hit, Mathf.Infinity))
			{
				currentMousePoint = hit.point;

				// Store where clicked
				if (Input.GetMouseButtonDown (0))
				{
					mouseDownPoint = hit.point;
					TimeLeftBeforeDeclareDrag = TimeLimitBeforeDeclareDrag;
					MouseDragStart = Input.mousePosition;
					StartedDrag = true;

				} else if (Input.GetMouseButton (0))
				{
					// if the user is not draggin lets do the test
					if (!UserIsDragging) 
					{
						// Test to see if the user is draggin
						TimeLeftBeforeDeclareDrag -= Time.deltaTime;
							if (TimeLeftBeforeDeclareDrag <= 0.0f || UserDraggingByPosition (MouseDragStart, Input.mousePosition))
								UserIsDragging = true;
					}
				} else if (Input.GetMouseButtonUp (0))
					{
						if (UserIsDragging)
							FinishDragOnThisFrame = true;
							UserIsDragging = false;
					}

					// Mouse click
					if (!UserIsDragging)
					{
						// Is it terrain?	
						if (hit.collider.name == "TerrainMain")
						{
							// right clicking creates target model if so spawn pointer
							if (Input.GetMouseButtonDown (1))
							{   
								GameObject TargetObj = Instantiate (Target, hit.point, Quaternion.identity) as GameObject;
								TargetObj.name = "Target Instantiate";
								RightClickPoint = hit.point;


							} else if (Input.GetMouseButtonUp (0)  DidUserClickLeftMouse (mouseDownPoint))
							{
								if (!Common.ShiftKeysDown ())
								DeselectGameobjectsIfSelected ();
							}
						} // end of terrain
					else {
						//hiting other things
						if (Input.GetMouseButtonUp (0)  DidUserClickLeftMouse (mouseDownPoint))
						{
							// Is the user hitting unit? Checks for the Unit Script
							if (hit.collider.gameObject.GetComponent<Unit>())
							{
								//are we selecting a different object?
								if (!UnitAlreadyInCurrentlySelectedUnits (hit.collider.gameObject))
								{
									// if shift key not down remove rest of units
									if (!Common.ShiftKeysDown ())
										DeselectGameobjectsIfSelected ();

										GameObject SelectedObj = hit.collider.transform.FindChild ("Selector").gameObject;
										SelectedObj.SetActive(true);
										// add unit to current units
										CurrentlySelectedUnits.Add (hit.collider.gameObject);

										//change the unit selected value to true
									hit.collider.gameObject.GetComponent<Unit>().Selected = true;
								} else {
									// unit is allreayd in the currently select units!
									//remove unit!
										if (Common.ShiftKeysDown ())
											RemoveUnitFromCurrentlySelectedUnits (hit.collider.gameObject);
								else {
										DeselectGameobjectsIfSelected ();
										GameObject SelectedObj = hit.collider.transform.FindChild ("Selector").gameObject;
										SelectedObj.SetActive (true);
										CurrentlySelectedUnits.Add (hit.collider.gameObject);
									}
								}

							} else {
								//if this object is not unit
								if (!Common.ShiftKeysDown ())
								{
									DeselectGameobjectsIfSelected ();
									GameObject SelectedObj = hit.collider.transform.FindChild ("Selector").gameObject;
									SelectedObj.SetActive (false);
								}
							}
						}
					}
					} else {
						if (Input.GetMouseButtonUp (0)  DidUserClickLeftMouse (mouseDownPoint))
							if (!Common.ShiftKeysDown ())
								DeselectGameobjectsIfSelected ();
						}//end of raycast
				} // end of dragging
				if(!Common.ShiftKeysDown() StartedDrag  UserIsDragging)
				{
					DeselectGameobjectsIfSelected();
					StartedDrag = false;
				}

		  	Debug.DrawRay (ray.origin, ray.direction * 1000, Color.yellow);
			
		if (UserIsDragging) 
				{
					//GUI Variables
					boxWidth = Camera.main.WorldToScreenPoint (mouseDownPoint).x - Camera.main.WorldToScreenPoint (currentMousePoint).x;
					boxHeight = Camera.main.WorldToScreenPoint (mouseDownPoint).y - Camera.main.WorldToScreenPoint (currentMousePoint).y;
					boxLeft = Input.mousePosition.x;
					boxTop = (Screen.height - Input.mousePosition.y) - boxHeight;
					
					if(Common.FloatToBool(boxWidth))
						if(Common.FloatToBool(boxHeight))
							boxStart = new Vector2(Input.mousePosition.x, Input.mousePosition.y + boxHeight);
						else
							boxStart = new Vector2(Input.mousePosition.x, Input.mousePosition.y);

					else 
						if(Common.FloatToBool(boxWidth))
							if(Common.FloatToBool(boxHeight))
								boxStart = new Vector2(Input.mousePosition.x + boxWidth, Input.mousePosition.y + boxHeight);
						else
								boxStart = new Vector2(Input.mousePosition.x + boxWidth, Input.mousePosition.y);
							

							
				boxFinish = new Vector2 (
										boxStart.x + Common.Unsigned(boxWidth),
										boxStart.y - Common.Unsigned(boxHeight)
										);

				}
	} // end of update function


	void LateUpdate()
	{
				UnitsInDrag.Clear ();

				//if user is dragging, or finished onm this fram, AND there are units to select on the screen
				if ((UserIsDragging || FinishDragOnThisFrame)  UnitsOnScreen.Count > 0) {
						//loop through thouse uunits on screen
						for (int i = 0; i < UnitsOnScreen.Count; i++) {
								GameObject UnitObj = UnitsOnScreen [i] as GameObject;
								Unit UnitsScript = UnitObj.GetComponent<Unit> ();
								GameObject SelectedObj = UnitObj.transform.FindChild ("Selector").gameObject;

								//if not allready in the dragged units
								if (!UnitAlreadyInDraggedUnits (UnitObj)) {
										if (UnitInsideDrag (UnitsScript.ScreenPos)) {
												SelectedObj.SetActive (true);
												UnitsInDrag.Add (UnitObj);
										}

					//unit is not in the drag!
					else {

												//remove the selected graphic, if units is not allready in currently selected units
												if (!UnitAlreadyInCurrentlySelectedUnits (UnitObj))
														SelectedObj.SetActive (false);
										}
								}
						}
				}

				if (FinishDragOnThisFrame) {
						FinishDragOnThisFrame = false;
						PutDraggedUnitsInCurrentlySelectedUnits ();
				}
		}

	void OnGUI()
	{

		//box width, hieght, top, left
		if(UserIsDragging)
		{

		GUI.Box(new Rect(boxLeft,
		                 boxTop,
			             boxWidth,
			             boxHeight), "", MouseDragSkin);
		}
				
	}
	
	#region Helper functions

	// is the user draggin, relative to the mouse start point
	public bool UserDraggingByPosition(Vector2 DragStartPoint, Vector2 NewPoint)
	{
		if(
			(NewPoint.x > DragStartPoint.x + clickDragZone || NewPoint.x < DragStartPoint.x - clickDragZone) ||
			(NewPoint.y > DragStartPoint.y + clickDragZone || NewPoint.y < DragStartPoint.y - clickDragZone)
			)
			return true; else return false;
	}


	// check if a user clicked mouse
		public bool DidUserClickLeftMouse(Vector3 hitPoint)
			{
					if (
			(mouseDownPoint.x < hitPoint.x + clickDragZone  mouseDownPoint.x > hitPoint.x - clickDragZone) 
			(mouseDownPoint.y < hitPoint.y + clickDragZone  mouseDownPoint.y > hitPoint.y - clickDragZone) 
			(mouseDownPoint.z < hitPoint.z + clickDragZone  mouseDownPoint.z > hitPoint.z - clickDragZone)
						)
						return true;
				else
						return false;
			}

	//deselects gameobject if selected
	public static void DeselectGameobjectsIfSelected()
		{
			if(CurrentlySelectedUnits.Count > 0)
			{
				for(int i = 0; i < CurrentlySelectedUnits.Count; i++)
				{
					GameObject ArrayListUnit = CurrentlySelectedUnits[i] as GameObject;
					ArrayListUnit.transform.FindChild("Selector").gameObject.SetActive(false);
					ArrayListUnit.GetComponent<Unit>().Selected = false;
					
				}
				
			CurrentlySelectedUnits.Clear();
			}
		}

	// check if a unit is allready in the currently selected units array list
	public static bool UnitAlreadyInCurrentlySelectedUnits(GameObject Unit)
	{
		if (CurrentlySelectedUnits.Count > 0) {
						for (int i = 0; i < CurrentlySelectedUnits.Count; i++) {
								GameObject ArrayListUnit = CurrentlySelectedUnits [i] as GameObject;
								if (ArrayListUnit == Unit)
										return true;
			
						}

						return false;
				} else
						return false;
			}

	// remove a unit from the currently selected units
	public void RemoveUnitFromCurrentlySelectedUnits(GameObject Unit)
	{
		if (CurrentlySelectedUnits.Count > 0) {
			for (int i = 0; i < CurrentlySelectedUnits.Count; i++) {
				GameObject ArrayListUnit = CurrentlySelectedUnits [i] as GameObject;
				if (ArrayListUnit == Unit)
					{
					CurrentlySelectedUnits.RemoveAt(i);
					ArrayListUnit.transform.FindChild("Selector").gameObject.SetActive(false);
					}
			}
			
			return;
		} else
			return;
		}

	// check if a unit is within screen space to deal with mouse drag selecting

	public static bool UnitWithinScreenSpace(Vector2 UnitScreenPos)
	{
		if(
			(UnitScreenPos.x < Screen.width  UnitScreenPos.y < Screen.height) 
			(UnitScreenPos.x > 0f  UnitScreenPos.y > 0f)
			)
			return true; else return false;
	}

	// lets remove a unit from screen space UnitsOnScreen array list
	public static void RemoveFromOnScreenUnits(GameObject Unit)
	{
		for (int i = 0; i < UnitsOnScreen.Count; i++)
		{
			GameObject UnitObj = UnitsOnScreen[i] as GameObject;
			if(Unit == UnitObj)
			{
				UnitsOnScreen.RemoveAt(i);
				UnitObj.GetComponent<Unit>().OnScreen = false;
				return;
			}
		}
			return;
	}

	//Is unit inside the drag?
	public static bool UnitInsideDrag(Vector2 UnitScreenPos)
	{
		if (
				(UnitScreenPos.x > boxStart.x  UnitScreenPos.y < boxStart.y) 
				(UnitScreenPos.x < boxFinish.x  UnitScreenPos.y > boxFinish.y))
						return true;
				else
						return false;
		}

	//Check if unit is allready in unit UnitDragArray List
	public static bool UnitAlreadyInDraggedUnits(GameObject Unit)
	{
		if (UnitsInDrag.Count > 0) {
			for (int i = 0; i < UnitsInDrag.Count; i++)
			{
				GameObject ArrayListUnit = UnitsInDrag [i] as GameObject;
				if (ArrayListUnit == Unit)
					return true;
				
			}
			return false;
		} else
			return false;
	}

	//take all units from UnitsInDrag, into  currentlySelected units(After a drag)
	public static void PutDraggedUnitsInCurrentlySelectedUnits()
	{
		if(!Common.ShiftKeysDown())
			DeselectGameobjectsIfSelected();
			
		if (UnitsInDrag.Count > 0)
		{
			for (int i = 0; i < UnitsInDrag.Count; i++)
			{
				GameObject UnitObj = UnitsInDrag [i] as GameObject;

				//if unit is not allready in currentlyselectedunits add it
				if(!UnitAlreadyInCurrentlySelectedUnits(UnitObj))
				{
				CurrentlySelectedUnits.Add(UnitObj);
				UnitObj.GetComponent<Unit>().Selected = true;
				}
			}
				
			UnitsInDrag.Clear();
		}
	}
	#endregion
		
}

any help would be appriciated. :expressionless:

Wow… massive derp guys i moved the ‘selector’ child which obv broke the script. Replaced the selector and its fixed :slight_smile: