Reputation:
I'm attempting to allow the players to open the door once they have located the key The 'hasKey' value is currently managing if the players has the key with either true or false. i now need to know how to use this 'hasKey' boolean in another script; i've been trying for hours and getting no where so i'll post my code below and maybe someone knows whats going on, thanks in advance!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class Detection : MonoBehaviour {
public GameObject objectToEnable;
public static bool Enabled = false;
public bool hasKey = false;
public DoorOpener _DoorOpener;
private void Update()
{
Debug.Log(hasKey);
if (Enabled)
{
objectToEnable.SetActive(true);
}
}
void OnMouseEnter()
{
Debug.Log("Enter");
}
void OnMouseExit()
{
Debug.Log("Exit");
}
void OnMouseUp()
{
Enabled = true;
hasKey = true;
Debug.Log("Pressed");
}
}
public class DoorOpener : MonoBehaviour
{
Animator animator;
bool JailDoorOpen;
public Detection _Detection;
void Start()
{
JailDoorOpen = false;
animator = GetComponent<Animator>();
}
void OnTriggerEnter(Collider JailDoorO)
{
if ((JailDoorO.gameObject.tag == "Player") && (_Detection.hasKey == true))
{
Debug.Log("Open Door");
JailDoorOpen = true;
jDoors("Open");
}
}
void jDoors (string direction)
{
animator.SetTrigger(direction);
}
}
enter code here
Upvotes: 0
Views: 1903
Reputation: 4343
In your second script you have declared:
public Detection _Detection;
but you have not said what _Detection
is assigned to. So it is just a blank instance of your Detection script. You need to reference the script that is attached to the specific object you are looking for.
For example if Detection and DoorOpener are both on the same gameobject you would do.
_Detection = gameObject.getComponent<Detection>();
or otherwise you could do something like...
_Detection = GameObject.FindWithTag('TagOfObjWithDetScript').getComponent<Detection>();
now the value of haskey
in DoorOpener matches the value of haskey
in the specific instance of the Detection script you are using.
Upvotes: 1
Reputation: 1074
If this is a local multiplayer game, you could store the boolean on the door object. You could also store it in a KeyManager
and access it through a static variable.
If this is a network multiplayer game, you're going to have to store the variable in a manager somewhere and update each client when the state of that boolean changes.
Upvotes: 0