So I am having an issue with my scripts that allow the player to pick up a key and open a door. The issue that I am having is coming from my Med Bay Door Manager script. The Med Bay Door Manager script is suppose to check the PickupItemScript on the FPS Controller to see whether the HasKey boolean variable is true or not when the player enters the trigger, but it’s not working due to a NullReferenceException on line 16 of the Med Bay Door Manager script that’s telling me that hasTheKey = itemPickup.HasKey object refernce is not set to an instance of an object. I am not sure how to fix this issue and it’s taken me a few days just to get the game to play again trying to figure out how to access another script. Help would be greatly appreciated.
This is my PickupItemScript:
using UnityEngine;
using System.Collections;
public class PickupItemScript : MonoBehaviour
{
public bool HasKey = false;
//player will get the key when the trigger is activated.
void OnTriggerEnter(Collider other)
{
//when the player collides with the pickup(square/the key)
//hasKey becomes true and the square is destroyed.
if (other.gameObject.tag == "Pickup")
{
HasKey = true;
Destroy (other.gameObject);
}
}
}
This is my Med Bay Door Manager Script:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class MedBayDoorManager : MonoBehaviour
{
public Door door1;
public Door door2;
public PickupItemScript itemPickup;
public bool hasTheKey;
//initialization
void Start ()
{
bool hasTheKey;
itemPickup = GetComponent<PickupItemScript>();
hasTheKey = itemPickup.HasKey;
}
// Doors open when trigger is activated.
void onTriggerEnter ()
{
// If player has the key door1 will open.
if (hasTheKey == true)
{
door1.OpenDoor();
}
// Door2 will open when trigger is activated for now.
// if statement will change when first if statement works.
if (door2 != null)
{
door2.OpenDoor ();
}
}
}