- Home /
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 = "";
}
}
}
}
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.
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
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
).
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?
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);
}
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.
Be sure to set the sound property to "3D" off, or it won't play.
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 = "";
}
}
}
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.
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
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");
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 ;)