How to wait a certain amount of seconds in C#
I have looked everywhere for an answer and but they never seem to work... I have tried using this: using UnityEngine; using UnityEngine.UI;
public class textBootUp : MonoBehaviour {
void Start () {
Text textLoad = GetComponent<Text>();
//Start of text change
textLoad.text = "";
System.Threading.Thread.Sleep(3000); //Attempt of a wait script
textLoad.text = "Loading";
}
}
The problem is when I launch the game in the editor, it takes 3 more seconds to load than normal and then when it has finished loading, it automatically changes the text to 'Loading'. Can anyone help?
Answer by Menyus777 · Jul 29, 2017 at 10:31 AM
To wait seconds in c# i have two ideas:
Time.deltatime in a loop (however i think this would not work in your case, and tend to freeze unity)
IEnumerator
yield return new waitforseconds(3f);
Example:
IEnumerator waiter_not_that_waiter_just_waiter(){
yield return new waitforseconds(3f);
//my code here after 3 seconds
}
To run a Couroutine u have to call this function like this, StartCouroutine(functionname);
Additional note: you would also need some sort of check/switch in your Start(). Like 'bool readyToGO' Set it to false before starting the Coroutine. Then have the IEnumerator set it to true when it finishes. Like 'readyToGo = true' where @$$anonymous$$enyus777 has 'my code ......'
https://docs.unity3d.com/$$anonymous$$anual/Coroutines.html
hope this helps
Answer by mircojanisch · Jul 29, 2017 at 01:38 PM
What I almost always do is to store the time in variable and then substract Time.deltaTime from it in the Update loop
public float time;
private float timeStore;
void Start(){
timeStore = time;
}
void Update(){
if (time > 0) {
time -= Time.deltaTime;
} else {
//Do Stuff
time = timeStore;
}
}
Fyi, To render the "Loading" text, It must be done outside of Start()
as it fires before the first frame is rendered. So waiting in Start()
will also delay the appearance of the text.
Answer by jandd661 · Jul 29, 2017 at 02:05 PM
Maybe something like this? Not great but you get the idea
public class textBootUp : MonoBehaviour {
void Start () {
Text textLoad = GetComponent<Text>();
//Start of text change
textLoad.text = "";
letsGo = false;
CoRunning = false;
//System.Threading.Thread.Sleep(3000); //Attempt of a wait script
//textLoad.text = "Loading";
}
}
void Update(){
if(!letsGo){
TextLoad.text = "Loading";
if(!CoRunning){
StartCoroutine(waiter_not_that_waiter_just_waiter);
}
return;
}
//below here will start after Coroutine
}
IEnumerator waiter_not_that_waiter_just_waiter(){
CoRunning = true;
//Do some stuff here while we wait
yield return new waitforseconds(3f);
//my code here after 3 seconds
textLoad.text = "";
letsGo = true;
CoRunning = false;
}
Answer by vishal · May 18, 2018 at 06:51 AM
Problem:
public class textBootUp : MonoBehaviour {
void Start () {
Text textLoad = GetComponent<Text>();
//Start of text change
textLoad.text = "";
System.Threading.Thread.Sleep(3000); //Attempt of a wait script
textLoad.text = "Loading";
}
}
Solution 1: public class textBootUp : MonoBehaviour {
void Start()
{
StartCoroutine(ChangeText(30f));
}
IEnumerator ChangeText(int halt)
{
Text textLoad = GetComponent<Text>();
//Start of text change
textLoad.text = "";
yield return new WaitForSeconds(halt);
textLoad.text = "Loading";
}
} Solution 2:
public class textBootUp : MonoBehaviour {
void Start ()
{
Text textLoad = GetComponent<Text>();
//Start of text change
textLoad.text = "";
Invoke("ChangeText", 30f);
}
void ChangeText()
{
textLoad.text = "Loading";
}
}