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;
			}
		}
	}

,

There are several problems here with your WaitForPlayer class.

  1. You are attempting to embed a function within a function.
  2. You are attempting to embed a function Unityscript within a C# function (you can not have JS and C# in the same script).
  3. 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 second Start() function that is embedded into the Update() function and make it a co-routine at the same time (neither Update() 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);
            }
        }
    }
}

Hello, I am just doing the same. Could someone tell me, what should I set in Inspector for GameObject waitScreen? Thanks a lot.