Door script only works one way?

I have a script that I want to function as a door opening, but I’m having some issues. Here’s the code…

var smooth = 2.0;
var DoorOpenAngle = 90.0;
var opened : GameObject;
var closed : GameObject;
var button1 : UnityEngine.UI.Graphic;
var button2 : UnityEngine.UI.Graphic;
var button1Background : UnityEngine.UI.Graphic;
var button2Background : UnityEngine.UI.Graphic;
var fadeTime : float = 0;
private var open : boolean;
private var enter : boolean;

private var defaultRot : Vector3;
private var openRot : Vector3;

function Start(){
    defaultRot = transform.eulerAngles;
    openRot = new Vector3 (defaultRot.x, defaultRot.y + DoorOpenAngle, defaultRot.z);
}

//Main function
function Update (){
    if(opened.activeSelf){
        //Open door
        transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, openRot, Time.deltaTime * smooth);
    }
   
    if(closed.activeSelf){
        //Close door
        transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, defaultRot, Time.deltaTime * smooth);
    }

    if(Input.GetButtonDown("Interact") && enter)
    if(opened.activeSelf){
        closed.SetActive (true);
        opened.SetActive (false);
    }
    if(Input.GetButtonDown("Interact") && enter)
        if(closed.activeSelf){
        opened.SetActive (true);
        closed.SetActive (false);
}

    if(enter)
    {
        button1.color = Color.Lerp (button1.color, Color.green, fadeTime * Time.deltaTime);
        button2.color = Color.Lerp (button2.color, Color.green, fadeTime * Time.deltaTime);
        button1Background.color = Color.Lerp (button1Background.color, Color.black, fadeTime * Time.deltaTime);
        button2Background.color = Color.Lerp (button2Background.color, Color.black, fadeTime * Time.deltaTime);
    }

    if(enter == false)
    {
        button1.color = Color.Lerp (button1.color, Color.clear, fadeTime * Time.deltaTime);
        button2.color = Color.Lerp (button2.color, Color.clear, fadeTime * Time.deltaTime);
        button1Background.color = Color.Lerp (button1Background.color, Color.clear, fadeTime * Time.deltaTime);
        button2Background.color = Color.Lerp (button2Background.color, Color.clear, fadeTime * Time.deltaTime);
    }
}

//Activate the Main function when player is near the door
function OnTriggerEnter (other : Collider){
    if (other.gameObject.tag == "Player") {
        enter = true;
    }
}

    //Deactivate the Main function when player is go away from door
    function OnTriggerExit (other : Collider){
        if (other.gameObject.tag == "Player"){
            enter = false;
    }
}

The issue is this. This line of the code works…

f(Input.GetButtonDown("Interact") && enter)
        if(closed.activeSelf){
        opened.SetActive (true);
        closed.SetActive (false);

But, this line doesn’t work, meaning that I can only open my door, not close it…

if(Input.GetButtonDown("Interact") && enter)
    if(opened.activeSelf){
        closed.SetActive (true);
        opened.SetActive (false);

what is this script attached to? one of “opened” or “closed”?

If it’s not firing, then either:
InputGetButtonDown(“Interact”) is false

  • or -
    enter is false
  • or -
    opened.activeSelf is false

You can easily Debug.Log those in Update.

1 Like

… or the script is attached to an inactive gameobject and no code is being run :slight_smile: