0

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

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

The two scripts are on different objects; the Detection script is on the CrateParent and the DoorOpener is on the JailDoor, i just need the jaildoor to register that 'hasKey' is true so that i can use it within an if statement to open the door
Ok, same process applies. Just need to reference the exact instance of the Detection script on your crateparent in whatever object needs its value.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.