- Home /
Scripting issue with opening a door with a key.
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 ();
}
}
}
Answer by GMarco · Mar 25, 2014 at 08:49 PM
this may sound obvious, but i have to ask it. Where are those scripts attached? for what its wrote both are attached to the same object, and for the exception you are getting, this given object doesn't have an instance of the PickupItemScript.
You should make a reference to the parent of this script first, and then retrieve it (this of course assuming the visibility of the methods is public, elsewhere you should start using setters or messengers). Hope it helps
The $$anonymous$$ed Bay Door $$anonymous$$anager Script is attached to the $$anonymous$$ed Bay door object and the PickupItemScript is attached to the FPS Controller object.
Answer by wraxul · Mar 25, 2014 at 08:57 PM
I'm not sure which scripts are attached to which objects, but if the game object with the MedBayDoorManager script attached does not also have a PickupItemScript attached, the GetComponent call is going to return null.
I assume both scripts are attached to the player? If that's the case, you'll most likely want to use GetComponentInChildren instead of GetComponent, provided the object with PickupItemScript is a child of the player. You'll also want to check the itemPickup.HasKey during the OnTriggerEnter rather than on Start provided that value can change during runtime.
Your answer
Follow this Question
Related Questions
Door opening with a key, my scripts and problem 1 Answer
Make doors open with different keys? 1 Answer
How to make 2 doors with 2 diffrent keys 2 Answers
Opening a door with a key 0 Answers