So I know this has been asked before but, I feel this only applies to my problem.
I get the "NullReferenceException: Object reference not set to an instance of an object" error. I usually know what to do but, at this point I cannot figure it out. I'm making a rts game and this code worked before but now gives me trouble. This is the Unit Script:
using UnityEngine;
using System.Collections;
public class Unit : MonoBehaviour {
public bool selected = false;
public CameraControl camControl;
public float speed = 10;
private Vector3 moveToDest = Vector3.zero;
public float floorOffset = 1;
public float stopDistanceOffset;
public Vector3 destination;
void Update () {
//UpdateMove();
if (GetComponent<Renderer>().isVisible && Input.GetMouseButtonUp(0))
{
Vector3 camPos = Camera.main.ScreenToViewportPoint(transform.position);
camPos.y = camControl.InvertMouseY(camPos.y);
selected = camControl.selection.Contains(camPos);
if (selected)
{
GetComponent<Renderer>().material.color = Color.red;
}
else
{
GetComponent<Renderer>().material.color = Color.white;
}
}
if(selected && Input.GetMouseButtonUp(1))
{
destination = camControl.GetDestination();
if (destination != null)
{
Debug.Log("Can Move");
moveToDest = destination;
NavMeshAgent agent = GetComponent<NavMeshAgent>();
agent.SetDestination(destination);
Vector3 direction = (moveToDest - transform.position).normalized;
transform.GetComponent<Rigidbody>().velocity = direction * speed;
}
else if(transform.position == destination)
{
transform.GetComponent<Rigidbody>().velocity = Vector3.zero;
}
}
}
}
Then here's the Camera Control Script:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class CameraControl : MonoBehaviour {
public Texture2D selectionHighlight = null;
public Rect selection = new Rect(0, 0, 0, 0);
private Vector3 startClick = -Vector3.one;
public float speed;
private static Vector3 moveToDestination = Vector3.zero;
private static List<string> passables = new List<string>() { "Floor" };
public RaycastHit hitTarget;
public GameObject target;
public GameObject targetInstance;
public float ZoomAmount = 0; //With Positive and negative values
public float MaxToClamp = 10;
public float ROTSpeed = 10;
void Update () {
ZoomAmount += Input.GetAxis("Mouse ScrollWheel");
ZoomAmount = Mathf.Clamp(ZoomAmount, -MaxToClamp, MaxToClamp);
var translate = Mathf.Min(Mathf.Abs(Input.GetAxis("Mouse ScrollWheel")), MaxToClamp -Mathf.Abs(ZoomAmount));
gameObject.transform.Translate(0, 0, translate * ROTSpeed * Mathf.Sign(Input.GetAxis("Mouse ScrollWheel")));
GameObject unit = GameObject.FindGameObjectWithTag("SelectableUnit");
if (Input.GetMouseButtonUp(1) && unit.GetComponent<Unit>().selected)
{
Destroy(targetInstance);
targetInstance = Instantiate(target, unit.GetComponent<Unit>().destination, Quaternion.identity) as GameObject;
}
else if(Input.GetMouseButtonDown(0))
{
Destroy(targetInstance);
}
Cleanup();
CheckCamera();
if (Input.GetKey(KeyCode.W))
{
transform.Translate(Vector3.up * speed);
}
if (Input.GetKey(KeyCode.S))
{
transform.Translate(Vector3.down * speed);
}
if (Input.GetKey(KeyCode.A))
{
transform.Translate(Vector3.left * speed);
}
if (Input.GetKey(KeyCode.D))
{
transform.Translate(Vector3.right * speed);
}
}
private void OnGUI()
{
if(startClick != -Vector3.one)
{
GUI.color = new Color(1, 1, 1, 0.5f);
GUI.DrawTexture(selection, selectionHighlight);
}
}
void CheckCamera()
{
if (Input.GetMouseButtonDown(0))
{
startClick = Input.mousePosition;
}
else if (Input.GetMouseButtonUp(0))
{
startClick = -Vector3.one;
}
if (Input.GetMouseButton(0))
{
selection = new Rect(startClick.x, InvertMouseY(startClick.y), Input.mousePosition.x - startClick.x, InvertMouseY(Input.mousePosition.y) - InvertMouseY(startClick.y));
if (selection.width < 0)
{
selection.x += selection.width;
selection.width = -selection.width;
}
if (selection.height < 0)
{
selection.y += selection.height;
selection.height = -selection.height;
}
}
}
public float InvertMouseY(float y)
{
return Screen.height - y;
}
private void Cleanup()
{
if (!Input.GetMouseButtonUp(1))
{
moveToDestination = Vector3.zero;
}
}
public Vector3 GetDestination()
{
if(moveToDestination == Vector3.zero)
{
RaycastHit hit;
Ray r = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(r, out hit))
{
while (!passables.Contains(hit.transform.gameObject.name))
{
if (!Physics.Raycast(hit.point + r.direction * 0.1f, r.direction, out hit))
{
break;
}
}
Debug.Log("Assigned Move Destination");
moveToDestination = hit.point;
hitTarget = hit;
}
}
return moveToDestination;
}
}
Most of the selection code is from a tutorial on Youtube. Unfortunately I don't know where I went wrong so I don't know where to revert back to.
@Happy Zomby The error was on line of the unit script with (vector3 campos = Camera.....). I sorta fixed it but the unit doesnt get selected. So not really. I think the error was that I put camera.main. I dont really know though. I renamed the camera in the hierarchy playerCam, if that has anything to do with it. As you can see underneath where the error is , selected = camControl.selection.Contains(camPos); that should become true and it's not so my unit's color doesn't change. Unity although doesnt give me an error.
Answer by Happy-Zomby · Apr 01, 2016 at 11:05 PM
Hi, When you get the error, you should also see which script and line, can you post it here?
Answer by UncleGubbsy · Apr 02, 2016 at 12:38 AM
I'm sorry to bother you with this question but, I solved the problem. Instead of referencing the player cam in the Inspector, I found it in code. Thanks for your concern though. Have a nice day.