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?
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
}
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;
}
}
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;
}
@Digestivbiscuitsm
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";
}
}