Help with C# Script

Hey everyone,

I need some help with a C# Script. I will start off by saying I absolutely suck at them. Now that that’s out of the way. I followed a tutorial on scripts for doors, all works like it should. However there’s one script I’d like to change to so that the trigger functions as a button for a VR game.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DoorInteractPressure : MonoBehaviour
{
    [SerializeField] private GameObject doorGameObject;
    private IDoor door;
    private float timer;

    private void Awake()
    {
        door = doorGameObject.GetComponent<IDoor>();
    }
    private void Update()
    {
        if (timer > 0)
        {
            timer -= Time.deltaTime;
            if (timer <= 0f)
            {
                door.CloseDoor();
            }
        }
    }
    private void OnTriggerEnter(Collider collider)
    {
        if (collider.gameObject.tag == "OpenAuto")
        {
            // Player entered collider!
            door.OpenDoor();
        }
    }
    private void OnTriggerStay(Collider collider)
    {
        if (collider.gameObject.tag == "OpenAuto")
        {
            // Player still on top of collider!
            timer = 5f;
        }
    }
}

What I need help with is changing this to a OnTriggerEnter script, that opens door the door, leaves it open, then closes the door once you enter the trigger again.

This would allow for me to use it as a button for a VR game.

@Penhoat, you need a boolean variable to keep track of is the door open or not. You do not need the OnTriggerStay function, or the Update function, so delete those.

If you only have one door, here is all you need:

  private bool isDoorOpen; // assumes the door is closed: this is false at the start

  private void OnTriggerEnter( Collider collider ) {
     if (collider.gameObject.tag == "OpenAuto") {
         if( isDoorOpen )  door.CloseDoor();
         else door.OpenDoor();
         isDoorOpen = !isDoorOpen; // set to the logical inverse of itself
     }
 }

if you have multiple doors, then you need a script on each door with isDoorOpen on it, and you need to get the script and check and set that variable when you collide with the door.