Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
2
Question by WirelessEStudios · Oct 09, 2016 at 02:17 PM · functionsetactivegetkeydowncoroutine errorsyeild

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


 


,

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by TBruce · Oct 09, 2016 at 02:49 PM

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);
             }
         }
     }
 }
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image WirelessEStudios · Oct 10, 2016 at 08:01 PM 0
Share

Thank you so much for you detailed and quick response! :D I will add your code and let you know how it turns out!

avatar image WirelessEStudios · Oct 10, 2016 at 08:24 PM 0
Share

I am getting error CS1519: Unexpected symbol `=' in class, struct, or interface member declaration at: private waitingToStartGame = true;

avatar image TBruce WirelessEStudios · Oct 10, 2016 at 08:26 PM 2
Share

Sorry, that is supposed to be

 private bool waitingToStartGame = true;

I have updated my answer above as well.

avatar image WirelessEStudios TBruce · Oct 10, 2016 at 09:11 PM 0
Share

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.

Show more comments
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

76 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Inactive in console even though its active in the inspector. 0 Answers

How to make a gameObject appear when I press spacebar down? 1 Answer

Function not responding to GetKeyDown 1 Answer

Failed to call function "" of class "". Calling function "" with no parameters but the function requires 1 2 Answers

Activate/Deactivate image with GetKeyDown? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges