Add Object to List of GameObjects C#

So I’ve seen a ton of similar questions, and none of the fixes have worked for me.

Essentially, I’m creating an RTS, and I’m dragging to select my units. I can determine what objects are in that area, but when I go to add them to a list so that I can perform actions with the selected unit, I get a null reference exception.

Before someone brings up that I need to reference the List before I can use it, I’ve already done that.

I’ll just go ahead and paste the whole script.

The part that matter to me here is that when the player lets up on the Left Mouse Key, it should Update Units to determine what we have, then check the area we ragged over. Empty out the old selected unit list. Then check each of our Units (which will eventually be cleaned up for performance) and determine if it is in the area to check.

This all works fine and I have some Debugging and GUI Rect’s that even confirm that all my positioning is correct for each object.

However, when I get to this section:

if (areaCheck.Contains(positionCheck))
				{
					Debug.Log("Adding unit.");
					Debug.Log("This unit is: " + myUnit.name);
					selectedUnits.Add(myUnit);
				}

It recognizes the object and sends it to the Debugger correctly (it prints the unit’s name, so clearly myUnit DOES have a space in memory as a gameObject. When I go to add it however with the next line “selectedUnits.Add(myUnit);” it throws me a Null Reference Exception.

And yes, “public List selectedUnits= new List();” I have already referenced the list, so that shouldn’t be the problem.


using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class script_Player : MonoBehaviour {

	// Use this for initialization
	
	public float camSpeed;
	public Camera mainCam;
	
	private Ray leftClickRay;
	private RaycastHit leftClickHit;
	private Vector2 mousePosDown;
	private float mouseXDown;
	private float mouseYDown;
	private Vector3 mouse3DHitDown;
	
	private Ray leftUpRay;
	private RaycastHit leftUpHit;
	private Vector2 mousePosUp;
	private float mouseXUp;
	private float mouseYUp;
	private Vector3 mouse3DHitUp;
	
	private float xDif;
	private float yDif;
	
	public LineRenderer lineRenderer;
	private Vector2 screenPointClick;
	private Vector2 screenPointUp;
	
	private float screenXDif;
	private float screenYDif;
	
	public GUISkin dragSelectSkin;
	
	public GameObject[] units;
	private Vector2 positionCheck;
	public List<GameObject> selectedUnits= new List<GameObject>();
	
	void Start () {
		mainCam = Camera.mainCamera;
		//selectedUnits = new List<GameObject>();
	}
	
	// Update is called once per frame
	void Update () {
		
		#region Movement
		if (Input.GetKey(KeyCode.W))
		{
			this.gameObject.transform.Translate(0,0,camSpeed);	
		}
		if (Input.GetKey(KeyCode.A))
		{
			this.gameObject.transform.Translate(-camSpeed,0,0);	
		}
		if (Input.GetKey(KeyCode.S))
		{
			this.gameObject.transform.Translate(0,0,-camSpeed);	
		}
		if (Input.GetKey(KeyCode.D))
		{
			this.gameObject.transform.Translate(camSpeed,0,0);	
		}
		#endregion
		
		#region Player Left Click Store
		//When Player left Clicks Get the Position of Click and store it
		if (Input.GetKey(KeyCode.Mouse0))
		{
			if (mouse3DHitDown == new Vector3(0,0,0))
			{
				mousePosDown = Input.mousePosition;
				mouseXDown = mousePosDown.x;
				mouseYDown = mousePosDown.y;
				leftClickRay = mainCam.ScreenPointToRay(new Vector3(mouseXDown, mouseYDown, 0));
				Debug.DrawRay (leftClickRay.origin, leftClickRay.direction * 100, Color.yellow);
				
				if (Physics.Raycast(leftClickRay, out leftClickHit, 300))
				{
					mouse3DHitDown = leftClickHit.point;
					Debug.Log(mouse3DHitDown);
				}	
			}
			
			mousePosUp = Input.mousePosition;
			mouseXUp = mousePosUp.x;
			mouseYUp = mousePosUp.y;
			leftUpRay = mainCam.ScreenPointToRay(new Vector3(mouseXUp, mouseYUp, 0));
			Debug.DrawRay (leftUpRay.origin, leftUpRay.direction * 100, Color.yellow);
			
			if (Physics.Raycast(leftUpRay, out leftUpHit, 300))
			{
				mouse3DHitUp = leftUpHit.point;
				Debug.Log(mouse3DHitUp);
			}	
			
			#region Create Debug Drag Selection Line
			xDif = mouse3DHitDown.x - mouse3DHitUp.x;
			yDif = mouse3DHitDown.z - mouse3DHitUp.z;
			
			Debug.DrawLine(mouse3DHitDown, (new Vector3(mouse3DHitDown.x - xDif, mouse3DHitDown.y, mouse3DHitDown.z)), Color.green);
			Debug.DrawLine(mouse3DHitDown, (new Vector3(mouse3DHitDown.x, mouse3DHitDown.y, mouse3DHitDown.z - yDif)), Color.green);
			Debug.DrawLine((new Vector3(mouse3DHitDown.x - xDif, mouse3DHitDown.y, mouse3DHitDown.z)), (new Vector3(mouse3DHitDown.x - xDif, mouse3DHitDown.y, mouse3DHitDown.z - yDif)), Color.green);
			Debug.DrawLine((new Vector3(mouse3DHitDown.x, mouse3DHitDown.y, mouse3DHitDown.z - yDif)), (new Vector3(mouse3DHitDown.x - xDif, mouse3DHitDown.y, mouse3DHitDown.z - yDif)), Color.green);
			#endregion
		}
		
		#endregion
		
		if (Input.GetKeyUp(KeyCode.Mouse0))
		{
			mouse3DHitUp = new Vector3(0,0,0);
			mouse3DHitDown = new Vector3(0,0,0);
			//screenYDif = 0;
			//screenXDif = 0;
			
			Debug.Log("Updating Units");
			UpdateUnits();
			
			Rect areaCheck = new Rect(screenPointClick.x, screenPointClick.y - screenYDif, -screenXDif, screenYDif);
			
			selectedUnits = null;
			
			foreach (GameObject myUnit in units)
			{
				positionCheck = mainCam.WorldToScreenPoint(myUnit.gameObject.transform.position);
				positionCheck.y = Screen.height - positionCheck.y;
				
				Debug.DrawRay(mainCam.WorldToScreenPoint(myUnit.gameObject.transform.position), Vector3.forward, Color.blue);
				
				Debug.Log("Screen Pos for Unit = " + positionCheck + " / Area to Check = " + areaCheck);
				
				if (areaCheck.Contains(positionCheck))
				{
					Debug.Log("Adding unit.");
					Debug.Log("This unit is: " + myUnit.name);
					selectedUnits.Add(myUnit);
					
				}
			}					
		}

//		if (Input.GetKeyUp(KeyCode.Mouse0))
//		{
//			mousePosUp = Input.mousePosition;
//			mouseXUp = mousePosUp.x;
//			mouseYUp = mousePosUp.y;
//			leftUpRay = mainCam.ScreenPointToRay(new Vector3(mouseXUp, mouseYUp, 0));
//			Debug.DrawRay (leftUpRay.origin, leftUpRay.direction * 100, Color.yellow);
//			
//			if (Physics.Raycast(leftUpRay, out leftUpHit, 300))
//			{
//				mouse3DHitUp = leftUpHit.point;
//				Debug.Log(mouse3DHitUp);
//			}	
//			
//			xDif = mouse3DHitDown.x - mouse3DHitUp.x;
//			yDif = mouse3DHitDown.z - mouse3DHitUp.z;
//			
//			Debug.DrawLine(mouse3DHitDown, (new Vector3(mouse3DHitDown.x + xDif, mouse3DHitDown.y, mouse3DHitDown.z)), Color.green);
//			Debug.DrawLine(mouse3DHitDown, (new Vector3(mouse3DHitDown.x, mouse3DHitDown.y, mouse3DHitDown.z + yDif)), Color.green);
//			Debug.DrawLine((new Vector3(mouse3DHitDown.x + xDif, mouse3DHitDown.y, mouse3DHitDown.z)), (new Vector3(mouse3DHitDown.x + xDif, mouse3DHitDown.y + yDif, mouse3DHitDown.z)), Color.green);
//			Debug.DrawLine((new Vector3(mouse3DHitDown.x, mouse3DHitDown.y, mouse3DHitDown.z + yDif)), (new Vector3(mouse3DHitDown.x + xDif, mouse3DHitDown.y + yDif, mouse3DHitDown.z)), Color.green);
//			Debug.DrawLine(mouse3DHitDown, mouse3DHitUp, Color.green);		
//		}
		
		#region Create Line Renderer
		xDif = mouse3DHitDown.x - mouse3DHitUp.x;
		yDif = mouse3DHitDown.z - mouse3DHitUp.z;
		
		Debug.Log("xDif = " + xDif);
		lineRenderer.transform.position = mouse3DHitDown;
		lineRenderer.SetPosition(0,mouse3DHitDown);
		lineRenderer.SetPosition(1, new Vector3(mouse3DHitDown.x - xDif, mouse3DHitDown.y, mouse3DHitDown.z));
		lineRenderer.SetPosition(2, new Vector3(mouse3DHitDown.x - xDif, mouse3DHitDown.y, mouse3DHitDown.z - yDif));
		lineRenderer.SetPosition(3, new Vector3(mouse3DHitDown.x, mouse3DHitDown.y, mouse3DHitDown.z - yDif));
		lineRenderer.SetPosition(4,mouse3DHitDown);
		#endregion
	}
	
	void OnGUI () {
		GUI.skin = dragSelectSkin;
		
		GUI.Box(new Rect(positionCheck.x, positionCheck.y,10,10), "");
		
		screenPointClick = mainCam.WorldToScreenPoint(mouse3DHitDown);
		screenPointUp = mainCam.WorldToScreenPoint(mouse3DHitUp);
		
		screenXDif = screenPointClick.x - screenPointUp.x;
		screenYDif = screenPointClick.y - screenPointUp.y;
		
		screenPointClick.y = Screen.height - Input.mousePosition.y;
		
		if (screenXDif != 0)
		{
			GUI.Box(new Rect(screenPointClick.x, screenPointClick.y - screenYDif, -screenXDif, screenYDif), "");
		}
	}
	
	void UpdateUnits () {
		units = GameObject.FindGameObjectsWithTag("Child");	
	}

}

Any advice for me? This has been driving me crazy for a couple days now.

You set selectedUnits to null in line 125. This causes the null reference exception later on in the same function. I think what you want instead is selectedUnits = new List< GameObject >(); instead