Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Input.GetKey("f") does not do anything when f i pressed

Within my, if function, the Input.GetKey("t") command does not work.
The Restart method is called when I remove the contents from the if function and place them outside.

using UnityEngine; using UnityEngine.SceneManagement;

public class GameManager : MonoBehaviour {
    bool gameHasEnded = false; 

    public float restartDelay = 2f;
    public GameObject LevelFailedUI;
    public GameObject LevelCompleteUI;

    public void CompleteLevel (){

        if (LevelFailedUI == true){
        LevelCompleteUI.SetActive(true);
        }
    }
    public void EndGame () {
        
        if (gameHasEnded == false)
        {
            gameHasEnded = true;
            LevelFailedUI.SetActive(true);
            if (Input.GetKey("t"))       //nothing happens when "t" is pressed.
            { 
                Invoke("Restart", restartDelay);
            }
        }

    }

    void Restart () 
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().name);
        LevelFailedUI.SetActive(false);
    } }

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

It won’t work because unity has to be inside the function and also hasn’t reached the check before reaching the if statement and you press the button which is basically impossible. You have to check for input constantly that is why it is done inside the Update function.

void Update() {
  if (Input.GetKey(KeyCode.T) && gameHasEnded) {
    Invoke("Restart", restartDelay);
  }
}
public void EndGame() {
  if (gameHasEnded == false) {
    gameHasEnded = true;
    LevelFailedUI.SetActive(true);
  }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading