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

Question Can i press twice a keyboard for different options?

Discussion in 'Getting Started' started by magaan2006, Dec 8, 2022.

  1. magaan2006

    magaan2006

    Joined:
    Jul 1, 2022
    Posts:
    10
    Im trying to create a basic pause menu, I can already open the pause menu with the esc keyboard, but i can't close it with the same button. my code is:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class EscapeToMenu : MonoBehaviour
    6. {
    7.     public GameObject menu;
    8.  
    9.     // Update is called once per frame
    10.     void Update()
    11.     {
    12.         if (Input.GetKeyDown(KeyCode.Escape))
    13.         {
    14.             menu.SetActive(true);
    15.         }
    16.        
    17.         if (menu.SetActive(true))
    18.         {
    19.             Input.GetKeyDown(KeyCode.Escape) = menu.SetActive(false);
    20.         }
    21.  
    22.     }
    23. }
    I've tried with this code, but it dosent work becuase of two errors, one being Cs0029: connot implicitly convert type 'void' to 'bool' and the other one being a Cs0131: The left-handed side of an assignmet must be a variable, property or indexer.
    Ive tried fixing this, but I couldn't find any solution.
     
  2. yigitgltkn

    yigitgltkn

    Joined:
    Nov 12, 2022
    Posts:
    1
    Did you try with getkeyup just after getkeydown ?
     
  3. AngryProgrammer

    AngryProgrammer

    Joined:
    Jun 4, 2019
    Posts:
    437
    Don't take this as bullying, but you need to learn the basics of programming.
    1. What is the bool type (https://www.w3schools.com/cs/cs_booleans.php).
    2. What is an if...else statement (https://www.w3schools.com/cs/cs_conditions.php).
    3. What is the returned value of the function (https://www.w3schools.com/cs/cs_method_parameters_return.php).
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class EscapeToMenu : MonoBehaviour
    6. {
    7.     public GameObject menu;
    8.  
    9.     void Update()
    10.     {
    11.         if (Input.GetKeyDown(KeyCode.Escape))
    12.         {
    13.             menu.SetActive(!menu.activeSelf);
    14.         }
    15.     }
    16. }
    How the code works? If I pressed Esc then set the inverse of the current status. If the menu is active it becomes inactive, if it is inactive it becomes active.
     
    Chubzdoomer and magaan2006 like this.