How do you wait for key press before a game starts?
Hi! I have a script that will show a UI and wait for the player to press the "S" key. Once that key is pressed it will enable the player and hide the UI. I am having problems with the function "Start". Any help would be appreciated.
Here is the script:
using UnityEngine;
using System.Collections;
public class WaitForPlayer : MonoBehaviour {
public GameObject waitScreen;
public GameObject mainPlayer;
// Use this for initialization
void Start () {
waitScreen.SetActive = (true);
mainPlayer.SetActive = (false);
}
void Update (){
function Start () {
while (true) {
while (!Input.GetKeyDown(KeyCode.S)) yield;
waitScreen.SetActive = (false); yield;
mainPlayer.SetActive = (true); yield;
}
}
}
,
Answer by TBruce · Oct 09, 2016 at 02:49 PM
There are several problems here with your WaitForPlayer
class.
You are attempting to embed a function within a function.
You are attempting to embed a function Unityscript within a C# function (you can not have JS and C# in the same script).
Start()
is a MonoBehaviour function that is called on the frame when a script is enabled just before any of the Update methods is called the first time, you are attempting to create a secondStart()
function that is embedded into theUpdate()
function and make it a co-routine at the same time (neitherUpdate() or Start()
can be co-routines).
You do not need to use co-routines to accomplish what you want. This should do what you are trying to do - Here is the updated and tested script
using UnityEngine;
using System.Collections;
public class WaitForPlayer : MonoBehaviour
{
public GameObject waitScreen;
public GameObject mainPlayer;
// flag to determine if we are waiting for uset input to start game
private bool waitingToStartGame = true;
// Use this for initialization
void Start ()
{
if (waitScreen != null)
{
waitScreen.SetActive(true);
}
else
{
waitingToStartGame = false;
Debug.LogError("waitScreen was not set in the inspector. Please set and try again");
}
if (mainPlayer != null)
{
mainPlayer.SetActive(false);
}
else
{
Debug.LogError("mainPlayer was not set in the inspector. Please set and try again");
}
}
void Update ()
{
// if the waitingToStartGame is enabled and the 'S' key has been pressed
if (waitingToStartGame && (Input.GetKeyDown(KeyCode.S)))
{
// set the flag to false so that will no longer be checking for input to start game
waitingToStartGame = false;
if (waitScreen != null)
{
waitScreen.SetActive(false);
}
if (mainPlayer != null)
{
mainPlayer.SetActive(true);
}
}
}
}
Thank you so much for you detailed and quick response! :D I will add your code and let you know how it turns out!
I am getting error CS1519: Unexpected symbol `=' in class, struct, or interface member declaration at: private waitingToStartGame = true;
Sorry, that is supposed to be
private bool waitingToStartGame = true;
I have updated my answer above as well.
Thanks again for your time! :D This helped a bunch. I saw your comment there about the waitScreen.SetActive and mainPlayer.SetActive. I must of accidentally deleted it.
Answer by Eviseek · Oct 21, 2017 at 04:46 PM
Hello, I am just doing the same. Could someone tell me, what should I set in Inspector for GameObject waitScreen? Thanks a lot.