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?
4 Answers
4
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";
}
}
To run a Couroutine u have to call this function like this,
– Menyus777StartCouroutine(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 @Menyus777 has 'my code ......' https://docs.unity3d.com/Manual/Coroutines.html hope this helps
– jandd661Then sorry, I can't help you. You should edit your question with this info, it's quite vital for your problem. If there is no mechanism for that, you could add your own ID system that your server could set on all clients when instantiating. I'm not sure if this is possible, just an idea
– Casiell