Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Door contoller with one button to open and close it

Discussion in 'Scripting' started by Cheeseboat, May 14, 2020.

  1. Cheeseboat

    Cheeseboat

    Joined:
    Oct 2, 2019
    Posts:
    8
    So as the title says, I am making a game that requires a door with a button to open and close it. I cant figure out how to script it so that it closes when you click the button again. Here's the script:

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

    public class DoorController : MonoBehaviour
    {

    public GameObject Door;
    public bool doorIsOpening;

    void Update()
    {
    if (doorIsOpening == true)
    {
    Door.transform.Translate(Vector3.up * Time.deltaTime * 800);
    //if the bool is true open the door

    }
    if (Door.transform.position.y > 30f)
    {
    doorIsOpening = false;
    //if the y of the door is > than 7 we want to stop the door
    }
    }
    void OnMouseDown()
    { //THIS FUNCTION WILL DETECT THE MOUSE CLICK ON A COLLIDER,IN OUR CASE WILL DETECT THE CLICK ON THE BUTTON

    doorIsOpening = true;
    //if we click on the button door we must start to open
    }

    }


    Please help me
     
  2. pinksheep8426

    pinksheep8426

    Joined:
    Jul 2, 2016
    Posts:
    5
    Add a bool called buttonState. With false being the state you want the button to have for the first press and true being the state you want the button to have for the second press.

    After doing the logic for whatever button press state is active at that time, do buttonState = !buttonState to invert it.
     
  3. Cheeseboat

    Cheeseboat

    Joined:
    Oct 2, 2019
    Posts:
    8
    Okay, I'll try that and send the code once I eat lunch, thanks!
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
  5. Cheeseboat

    Cheeseboat

    Joined:
    Oct 2, 2019
    Posts:
    8
    @pinksheep8426
    I added the stuff as you said and got this:
    public class DoorController : MonoBehaviour
    {

    public GameObject Door;
    public bool doorIsOpening;
    public bool buttonState;

    void Update()
    {
    if (doorIsOpening == true)
    {
    Door.transform.Translate(Vector3.up * Time.deltaTime * 800);
    //if the bool is true open the door

    }
    if (Door.transform.position.y > 30f)
    {
    doorIsOpening = false;
    //if the y of the door is > than 7 we want to stop the door
    }
    if (buttonState = false);
    {
    buttonState = !buttonState;
    }
    }
    void OnMouseDown()
    { //THIS FUNCTION WILL DETECT THE MOUSE CLICK ON A COLLIDER,IN OUR CASE WILL DETECT THE CLICK ON THE BUTTON

    doorIsOpening = true;
    //if we click on the button door we must start to open
    }

    }

    I don't exactly know C# that well, so based on when I tried playing the code, it didn't work, could you point out, or anyone else, where and what I did wrong? Thanks.