Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by KnightRiderGuy · Dec 11, 2014 at 01:03 AM · waitforsecondsaudio.playoneshot

C# Wait For Seconds

I'm not really good with C# so I'm having a hard time trying to figure out how to get this script to wait for about 3 or 4 seconds before allowing access to the next scene to be loaded. I have also been trying to figure out how to get a sound to play before launching the next scene too. What might make this one hard to figure out is that each of the log in buttons (and there are many) all use this same script. Here is the code I'm trying to work with.

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 
 public class readkey : MonoBehaviour
 {
     private Text inputText;
     private Text outputText;
     private Text acceptedText;
     
     string secret = "KITT2000";
     
     void Start ()
     {
         inputText = GameObject.Find ("InputText").GetComponent<Text> ();
         outputText = GameObject.Find ("OutputText").GetComponent<Text> ();
         inputText.text = "";
     }
     public void readButton (string c)
     {
         inputText.text += c;
         outputText.text = new string ('*', inputText.text.Length);
         if (inputText.text.Length == secret.Length) {
             if (inputText.text == secret) {
                 acceptedText = GameObject.Find ("AcceptedText").GetComponent<Text> ();
 
                 Application.LoadLevel ("SystemStatusScreen");
             } else {
                 acceptedText.text = "ACCESS\nGRANTED";
                 outputText.text = "WRONG PASS\nTRY AGAIN";
                 inputText.text = "";
             }
         }
     }
 }
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

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by ahmedbenlakhdhar · Dec 11, 2014 at 01:26 AM

Try to put this instead ( timeToWait is a float expressed in seconds ):

 if (inputText.text == secret) {
    acceptedText = GameObject.Find ("AcceptedText").GetComponent<Text> ();
    Invoke("loadLevel", timeToWait);
 }

And create a function like this:

 void loadLevel(){
     Application.LoadLevel ("SystemStatusScreen");
 }

As you can notice, loadLevel() will be called after the value of timeToWait in seconds.

Comment
Add comment · Show 9 · 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 KnightRiderGuy · Dec 11, 2014 at 01:43 AM 0
Share

I must be adding it wrong as it gives me errors??

avatar image ahmedbenlakhdhar · Dec 11, 2014 at 01:44 AM 0
Share

Can you show the errors?

avatar image KnightRiderGuy · Dec 11, 2014 at 01:53 AM 0
Share

Assets/Scripts/readkey.cs(35,30): error CS1547: $$anonymous$$eyword void' cannot be used in this context Assets/Scripts/readkey.cs(35,31): error CS1525: Unexpected symbol (', expecting )', ,', ;', [', or `='

Assets/Scripts/readkey.cs(39,1): error CS8025: Parsing error

avatar image ahmedbenlakhdhar · Dec 11, 2014 at 01:57 AM 0
Share

You have to create the new function under the class, neither outside the class nor in another function (like what you did with public void readButton ).

avatar image KnightRiderGuy · Dec 11, 2014 at 01:58 AM 0
Share

Not sure I'm understanding I have tried pasting it in a few different places and I still get errors. Can you put a sample of what you mean?

Show more comments
avatar image
0

Answer by blueLED · Dec 11, 2014 at 02:31 AM

You can also use the Time class like this:

 if (!setTimer) {
 
    // only set the timer once
    setTimer = true;
 
    // record what is the current time and add 1.5 seconds, for example
    waitUntilThisTime = Time.time + 1.5f;
 
 }
 
 if (Time.time > waitUntilThisTime) {
 
    // if the current time has caught up to the wait time, start next scene
    Application.LoadLevel(Application.loadedLevel + 1);
 
 }

   
Comment
Add comment · Show 13 · 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 blueLED · Dec 11, 2014 at 02:34 AM 0
Share

To play your sound, add this line in the "setTimer" if-block:

 AudioSource.PlayClipAtPoint(nextSceneSound, mainCamera.transform.position); 

Where "mainCamera" is a reference to the main camera in your scene.

avatar image blueLED · Dec 11, 2014 at 02:35 AM 0
Share

Be sure to set the sound property to "3D" off, or it won't play.

avatar image KnightRiderGuy · Dec 11, 2014 at 04:42 AM 0
Share

O$$anonymous$$ as near as I can tell I typed it in just like you said and I get a ton of errors??

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 
 public class readkey : $$anonymous$$onoBehaviour
 {
     private Text inputText;
     private Text outputText;
     private Text acceptedText;
     
     string secret = "$$anonymous$$ITT2000";
 
     void loadLevel(){
         Application.LoadLevel ("SystemStatusScreen");
     
     void Start ()
     {
         inputText = GameObject.Find ("InputText").GetComponent<Text> ();
         outputText = GameObject.Find ("OutputText").GetComponent<Text> ();
         inputText.text = "";
     }
     public void readButton (string c)
     {
         inputText.text += c;
         outputText.text = new string ('*', inputText.text.Length);
         if (inputText.text.Length == secret.Length) {
                 if (inputText.text == secret) {
                     acceptedText = GameObject.Find ("AcceptedText").GetComponent<Text> ();
                     if (!setTimer) {
                         setTimer = true;
 
                         waitUntilThisTime = Time.time + 1.5f;
                     }
                     if (Time.time > waitUntilThisTime) {
 
                 
                     Application.LoadLevel (Application.LoadLevel +1);
                     } else {
                         acceptedText.text = "ACCESS\nGRANTED";
                         outputText.text = "WRONG PASS\nTRY AGAIN";
                         inputText.text = "";
                         }
                     }
     }
avatar image blueLED · Dec 11, 2014 at 05:33 AM 0
Share

Yes, there would be many errors written this way. Those variables have to be declared first. Also, look at the code I wrote, the parameter is "Application.loadedLevel" not "Application.LoadLevel". $$anonymous$$ore importantly time should be checked every frame in the Update() function, not only when the button is clicked. It will never work if you only check the time once. Are you using an Update() function anywhere? Put the time check there.

avatar image blueLED · Dec 11, 2014 at 05:42 AM 0
Share

Basically, once the player clicks the button, you want to set the time to wait until, right, so you understand that logic. But what you need to do next is put this in your Update():

 if (setTimer && Time.time > timeToWaitUntil) Application.LoadLevel(Application.loadedLevel + 1);

You see, so every frame, after you clicked the button and setTimer is true, it start to check if the current time has elapsed past the wait time, then it will load the next level.

Again, be sure to declare the variables:

 private bool setTimer = false; // make sure it's false at the start
 private float waitUntilThisTime = 0f; // no time set at start
Show more comments
avatar image
0

Answer by Jess.Loboz · Dec 12, 2014 at 01:20 PM

You can Do this

IEnumerator LoadSceneAfter4Seconds() {

//Some stuff like Play audio

yield return new WaitForSeconds(4);

//Load Scene here

}

Call this function as follows StartCoroutine("LoadSceneAfter4Seconds");

Comment
Add comment · Show 1 · 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 KnightRiderGuy · Dec 12, 2014 at 01:58 PM 0
Share

Thanks Jess, I think I might have tried this already and it probably didn't work, might have been I was typing it in wrong, but I'm trying to use it with the new UI buttons on a canvas and a panel. $$anonymous$$y code is a bit of a dogs dinner after all of that mucking about but it works just NOT the wait timer crap so I figured I better not mess with it any more ;) I'n no bloody good at C# and have enough of a fun time with Java ;)

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

28 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

Related Questions

Sound plays multiple times after StartCoroutine when it should only play once. 0 Answers

yield return new WaitForSeconds is taking to long? 1 Answer

how do i write this: apply a negative force value on my object (to make it slow down its velocity), but i don't want it to go past 0, which ends up making the object travel in reverse. 1 Answer

Prefab Instantiation 1 Answer

Press Key for higher TimeScale and WaitForSeconds for lower TimeScale 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