I cannot figure out how to solve this NullReferenceException: Object reference not set to an instance of an object error.
I know it occurs because of my void OnTriggerEnter(Collider other) {, where other is causing the error. On that function “other” is holding a script that the rest of the code in that function is looking for. But when the object that is holding the script with the OnTrigger function mentioned hits a trigger collider that is not holding the script my function is looking for, I get this error. But what I don’t get is that I have the if (other.transform.tag == “PlayerSpace”) { to make sure the function runs only if it see’s that tag, and every object with that tag has the script the function is looking for.
The error happens at line 71.
( I tried submitting this before, someone told me I had to use the if (other != null) {, then took down my question. But I am still having this error.)
Please help. I’ve also made an image to help explain.
Also, if there is anything I can lookup to help me understand how to avoid this sort of error.
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float speed = 20.0f;
public Rigidbody rb;
public GameObject LaneSwicthPlayer;
public bool moveNorth;
public bool moveEast;
public bool moveSouth;
public bool moveWest;
public bool turn;
public int PlayerMoves = -1;
private Vector3 LaneSwicthPlayerPosition;
// Declaring variables to GameObjects that are Turn Based ( Will later assign their Turn to true on Trigger event)
private GameObject[] patientHealthBars;
private GameObject[] obstacleCars;
// Use this for initialization
void Start () {
Invoke("playerTurnStartDelay", 1);
rb = GetComponent<Rigidbody>();
LaneSwicthPlayer = GameObject.FindGameObjectWithTag("PlayerMesh");
// LaneSwicthPlayerPosition = LaneSwicthPlayer.transform.position;
// Initializing variables to Turn based GameObject Tags
patientHealthBars = GameObject.FindGameObjectsWithTag("PatientHealth");
obstacleCars = GameObject.FindGameObjectsWithTag("ObstacleCarCollider");
}
// Update is called once per frame
void Update () {
inputMovement();
}
//Gives turn to ObstacleCar when Player enters a PlayerSpace, removes turn from Player when Player reaches this
void OnTriggerEnter(Collider other) {
if (other != null) {
if (other.transform.tag == "PlayerSpace") {
// GameObject.FindGameObjectWithTag ("ObstacleCarCollider").GetComponent<ObstacleCarController> ().turn = true;
// GameObject.FindGameObjectWithTag ("ObstacleCarCollider").GetComponent<ObstacleCarController_wayPoints> ().turn = true;
foreach (GameObject car in obstacleCars) {
car.GetComponent<ObstacleCarController_wayPoints> ().turn = true;
}
//finds all patient health bars and sets them to true to decrease patient health
foreach (GameObject healthBar in patientHealthBars) {
healthBar.GetComponent<HealthDecrease> ().turn = true;
}
GameObject.FindGameObjectWithTag ("PatientHealth").GetComponent<HealthDecrease> ().turn = true;
turn = false;
PlayerMoves ++;
transform.position = other.transform.position;
}
moveNorth = other.gameObject.GetComponent<SpaceNavBoolean> ().moveNorth;
moveEast = other.gameObject.GetComponent<SpaceNavBoolean> ().moveEast;
moveSouth = other.gameObject.GetComponent<SpaceNavBoolean> ().moveSouth;
moveWest = other.gameObject.GetComponent<SpaceNavBoolean> ().moveWest;
} else
{
Debug.Log("worked");
}
}
//Used because Player Turn will not become True on Start of game. The delay will give Turn to player and not to Obstacle
void playerTurnStartDelay() {
turn = true;
}
void inputMovement() {
if (Input.GetKeyDown (KeyCode.UpArrow) && moveNorth == true && turn == true)
{
transform.localEulerAngles = new Vector3 (0, 0, 0);
rb.velocity = new Vector3 (0, 0, speed);
LaneSwicthPlayer.transform.localPosition = new Vector3 (.0125f, 0, 0);
}
if(Input.GetKeyDown(KeyCode.RightArrow) && moveEast == true && turn == true)
{
transform.localEulerAngles = new Vector3 (0, 90, 0);
rb.velocity = new Vector3 (speed, 0, 0);
LaneSwicthPlayer.transform.localPosition = new Vector3 (.0125f, 0, 0);
}
if(Input.GetKeyDown(KeyCode.DownArrow) && moveSouth == true && turn == true)
{
transform.localEulerAngles = new Vector3 (0, 180, 0);
rb.velocity = new Vector3 (0, 0, -speed);
LaneSwicthPlayer.transform.localPosition = new Vector3 (.0125f, 0, 0);
}
if(Input.GetKeyDown(KeyCode.LeftArrow) && moveWest == true && turn == true)
{
transform.localEulerAngles = new Vector3 (0, 270, 0);
rb.velocity = new Vector3 (-speed, 0, 0);
LaneSwicthPlayer.transform.localPosition = new Vector3 (.0125f, 0, 0);
}
}
}