Infinite Time inbetween if statements?
So im trying to make a script for a exit box and the player presses the esc key that brings up the exit box and player movement is disabled. But in order for it to work the player has to press the Esc key and A or D to select the choice and enter all at the same. Is there a way to put infinite time between if statements? Also this script is possibly longer than it needs to be because I dont really understand the foreach transform child thing. Also I tried disabling the Playermovement script but it didn't work.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class EscapeScript : MonoBehaviour {
public Animator EscAnimNo;
public Animator EscAnimYes;
public float FadeOutTimer;
public GameObject ExitBox;
public GameObject Yes;
public GameObject No;
public GameObject Text;
public GameObject MText;
public Playermove PM;
void Start () {
ExitBox = GameObject.Find ("ExitBox");
Yes = GameObject.Find ("EscYes");
No = GameObject.Find ("EscNo");
Text = GameObject.Find ("Exit Text");
MText = GameObject.Find ("Makesuretext");
ExitBox.SetActive (false);
Yes.SetActive (false);
No.SetActive (false);
Text.SetActive (false);
MText.SetActive (false);
//Get all needed Children and disable them
}
//Fade out Animation
IEnumerator Exiter () {
yield return new WaitForSeconds (FadeOutTimer);
SceneManager.LoadScene ("Start Screen");
}
void Update () {
// enable the exit game box
if (Input.GetKey(KeyCode.Escape)) { //infinite time needed here
ExitBox.SetActive (true);
Yes.SetActive (true);
No.SetActive (true);
Text.SetActive (true);
MText.SetActive (true);
PM.Equals (false);
//Disable PlayermovementScript
if (Input.GetKey(KeyCode.A)) {
EscAnimYes.SetBool ("C-Selected", true);
EscAnimNo.SetBool ("C-Selected", true);
Debug.Log ("Check Select");
//time between A and Enter needs to be infinite
if (Input.GetKey(KeyCode.KeypadEnter)) {
StartCoroutine (Exiter ());
}
if (Input.GetKey (KeyCode.D)) {
EscAnimYes.SetBool ("X-Selected", true);
EscAnimNo.SetBool ("X-Selected", true);
Debug.Log ("X Selected");
//time between D and Enter needs to be infinite
if (Input.GetKey (KeyCode.KeypadEnter)) {
ExitBox.SetActive (false);
Yes.SetActive (false);
No.SetActive (false);
Text.SetActive (false);
MText.SetActive (false);
PM.Equals (true);
//Enable playermovent after exitbox is exited
}
}
}
}
}
}
Answer by vinilly · May 11, 2018 at 10:12 PM
Were you referring to Unity UI element?
To disable the player movement and pause the game.. use;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class EscapeScript : MonoBehaviour {
public Animator EscAnimNo;
public Animator EscAnimYes;
public float FadeOutTimer;
public GameObject ExitBox;
public GameObject Yes;
public GameObject No;
public GameObject Text;
public GameObject MText;
public Playermove PM;
void Start () {
ExitBox = GameObject.Find ("ExitBox");
Yes = GameObject.Find ("EscYes");
No = GameObject.Find ("EscNo");
Text = GameObject.Find ("Exit Text");
MText = GameObject.Find ("Makesuretext");
ExitBox.SetActive (false);
Yes.SetActive (false);
No.SetActive (false);
Text.SetActive (false);
MText.SetActive (false);
//Get all needed Children and disable them
}
//Fade out Animation
IEnumerator Exiter () {
yield return new WaitForSeconds (FadeOutTimer);
SceneManager.LoadScene ("Start Screen");
}
void Update () {
// enable the exit game box
if (Input.GetKey(KeyCode.Escape)) { Time.timeScale = 0.0f; //NEW
ExitBox.SetActive (true);
Yes.SetActive (true);
No.SetActive (true);
Text.SetActive (true);
MText.SetActive (true);
PM.Equals (false);
//Disable PlayermovementScript
if (Input.GetKey(KeyCode.A)) {
EscAnimYes.SetBool ("C-Selected", true);
EscAnimNo.SetBool ("C-Selected", true);
Debug.Log ("Check Select");
//time between A and Enter needs to be infinite
if (Input.GetKey(KeyCode.KeypadEnter)) {
StartCoroutine (Exiter ());
}
if (Input.GetKey (KeyCode.D)) {
EscAnimYes.SetBool ("X-Selected", true);
EscAnimNo.SetBool ("X-Selected", true);
Debug.Log ("X Selected");
//time between D and Enter needs to be infinite
if (Input.GetKey (KeyCode.KeypadEnter)) {
ExitBox.SetActive (false);
Yes.SetActive (false);
No.SetActive (false);
Text.SetActive (false);
MText.SetActive (false);
PM.Equals (true);
Time.timeScale = 1.0f; //NEW
}
}
}
}
}
}
Well pausing the game like that doesn't work with the menu and the playermovent still kinda works also there's still no infinite time between the if statements.
Answer by unity_2020higginbothamdarby · May 15, 2018 at 09:17 PM
Alright I did manage to fix it by checking if the G key was pressed if it wasn't it runs again
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class EscapeScript : MonoBehaviour {
public Animator EscAnimNo;
public Animator EscAnimYes;
public float FadeOutTimer;
public GameObject ExitBox;
public GameObject Yes;
public GameObject No;
public GameObject Text;
public GameObject MText;
public bool PressedG;
public bool EsckeyPressed;
void Start () {
ExitBox = GameObject.Find ("ExitBox");
Yes = GameObject.Find ("EscYes");
No = GameObject.Find ("EscNo");
Text = GameObject.Find ("Exit Text");
MText = GameObject.Find ("Makesuretext");
PressedG = false;
EsckeyPressed = false;
ExitBox.SetActive (false);
Yes.SetActive (false);
No.SetActive (false);
Text.SetActive (false);
MText.SetActive (false);
//Get all needed Children and disable them
}
//Fade out Animation
IEnumerator Exiter () {
yield return new WaitForSeconds (FadeOutTimer);
SceneManager.LoadScene ("Start Screen");
}
void Update () {
// enable the exit game box
if (Input.GetKey(KeyCode.Escape) || EsckeyPressed == true && PressedG == false) {
EsckeyPressed = true;
ExitBox.SetActive (true);
Yes.SetActive (true);
No.SetActive (true);
Text.SetActive (true);
MText.SetActive (true);
if (Input.GetKey (KeyCode.A)) {
EscAnimYes.SetBool ("C-Selected", true);
EscAnimNo.SetBool ("C-Selected", true);
Debug.Log ("Check Select");
//time between A and Enter needs to be infinite
if (Input.GetKey (KeyCode.G)) {
StartCoroutine (Exiter ());
PressedG = true;
}
}
if (Input.GetKey (KeyCode.D)) {
EscAnimYes.SetBool ("X-Selected", true);
EscAnimNo.SetBool ("X-Selected", true);
Debug.Log ("X Selected");
//time between D and Enter needs to be infinite
if (Input.GetKey (KeyCode.G)) {
ExitBox.SetActive (false);
Yes.SetActive (false);
No.SetActive (false);
Text.SetActive (false);
MText.SetActive (false);
PressedG = true;
}
}
if (Input.GetKey(KeyCode.G) == false) {
PressedG = false;
}
}
}
}
But now the animation is a bit weird when you press A it goes switching back and forth but pressing the D key is fine sometimes or it does the animation switching and now A key is fine.