Errors with Script C# [GetComponent, FindGameObject]

I have some code that im using to simulate a player getting into one of my spaceships but it is not working, anyone know why. I am a beginner so i would not know the way to fix it unless i was told. Thank you




using UnityEngine;
using System.Collections;

public class GetInOutShip : MonoBehaviour {

	public Camera mainCamera;
	public Camera shipCamera;

	public Component getInArea;

	public GameObject player;
	public GameObject ship;

	private ShipController shipControls;
	private FirstPersonController playerControls;

	bool inShip = false;

	void Start () {

		getInArea = GetComponentInChildren<BoxCollider>();

		player = GameObject.FindGameObjectWithTag("Player");
		ship = GameObject.FindGameObjectWithTag ("Ship");

		shipControls = GetComponent <ShipController>();
		playerControls = GetComponent <FirstPersonController>();

		shipCamera.enabled = false;
		mainCamera.enabled = true;

		shipControls.enabled = false;
		playerControls.enabled = true;

	void OnTriggerEnter (Collider collision) {

			inShip = true;
			playerControls.enabled = !playerControls.enabled;
			shipControls.enabled = !shipControls.enabled;
			shipCamera.enabled = !shipCamera.enabled;
			mainCamera.enabled = !mainCamera.enabled;

	void OnTriggerExit (Collider collision) {
			inShip = false;


Some errors im getting:


  • NullReferenceException: Object reference not set to an instance of
    an object GetInOutShip.OnTriggerEnter
    (UnityEngine.Collider collision) (at
  • NullReferenceException: Object reference not set to an instance of
    an object GetInOutShip.Start () (at

Both those lines reference the playerControls so best guess is the player controls are on a different gameObject to the one this script is on. Most likely the player so try this:

playerControls = player.GetComponent <FirstPersonController>();