1
\$\begingroup\$

I have a key and door system working, the character(ball) picks up the key and when it is within the collider it sets the text("press E to open") on the screen to active so that they can open the door. This only needs to be there while the door is closed. How Would I fix this. Thank you so much in advance.

    //GameObject E is the canvas text to instruct the player to press E to open the door.
public GameObject E;
//Door is the door itself, I assigned this manually because the door consists of the doorframe and door. 
public GameObject Door;
private bool Unlock;
// Use this for initialization
void Start ()
{
    E.SetActive(false);
    Unlock = false;
}

// Update is called once per frame
void Update ()
{
    if (Unlock)
    {
        if (Input.GetKeyDown("e"))
        {
            Door.GetComponent<Animation>().Play();
            E.SetActive(false);
            CodeManager.GotKey = false;
        }
    }
}
void OnTriggerEnter(Collider other)
{
        if (CodeManager.GotKey)
        {
            if(other.gameObject.CompareTag("Player"))
            {
            E.SetActive(true);
            Unlock = true;
            }
        }
}
\$\endgroup\$
1
\$\begingroup\$

The simple solution is to add a bool property to the door indicating if its open or not. Then you simply check:

if(ohter.gameObject.CompareTag("Player") && Door.isClosed)
{
//Do logic here
}

You can even set the isClosed to false when the open animation has finished using a Animation Event.

\$\endgroup\$
  • \$\begingroup\$ Thank you very much, the function does work and with this it checks logic first. The "press e" text is still active when the player moves out of the collider, anyway around this? Thank you in advance ^^ \$\endgroup\$ – Hulio VanQuest Apr 25 '16 at 20:34
  • 1
    \$\begingroup\$ @HulioVanQuest docs.unity3d.com/ScriptReference/… should solve your problem. Just disable the text in OnTriggerExit. Also dont forget to add a check to where you enable to text to check if the door is open or closed. \$\endgroup\$ – Uri Popov Apr 26 '16 at 10:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.