[SOLVED]My UI text don't update from script
I have the following script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class SceneFader : MonoBehaviour {
public Image img;
public AnimationCurve curve;
private float _progress = 0f;
public Text loadingText;
private void Start()
{
StartCoroutine(FadeIn());
}
public void FadeTo(string scene)
{
StartCoroutine(FadeOut(scene));
}
IEnumerator FadeIn()
{
float t = 1f;
while(t > 0f)
{
t -= Time.deltaTime;
float a = curve.Evaluate(t);
img.color = new Color(0f, 0f, 0f, a);
yield return 0;
}
}
IEnumerator FadeOut(string scene)
{
AsyncOperation operation = SceneManager.LoadSceneAsync(scene);
operation.allowSceneActivation = false;
while(_progress < 1f)
{
_progress = Mathf.Clamp01(operation.progress / 0.9f);
loadingText.text = "Loading... " + (int)(_progress * 100f) + "%";
//Debug.Log("Loading... " + (int)(_progress * 100f) + "%");
yield return null;
}
float t = 0f;
while (t < 1f)
{
t += Time.deltaTime;
float a = curve.Evaluate(t);
img.color = new Color(0f, 0f, 0f, a);
loadingText.color = new Color(1f, 1f, 1f, a);
yield return 0;
}
//SceneManager.LoadScene(scene);
operation.allowSceneActivation = true;
}
}
and as the title say, the loadingText don't update to show what is in the function
Answer by DaresFireson · Apr 24, 2019 at 08:53 AM
It seems my text was not big enough to display all the content
Answer by Chris333 · Apr 08, 2019 at 09:55 AM
How to load a scene async and display the progress:
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class AsyncOperationProgressExample : MonoBehaviour
{
public Text m_Text;
public Button m_Button;
void Start()
{
//Call the LoadButton() function when the user clicks this Button
m_Button.onClick.AddListener(LoadButton);
}
void LoadButton()
{
//Start loading the Scene asynchronously and output the progress bar
StartCoroutine(LoadScene());
}
IEnumerator LoadScene()
{
yield return null;
//Begin to load the Scene you specify
AsyncOperation asyncOperation = SceneManager.LoadSceneAsync("Scene3");
//Don't let the Scene activate until you allow it to
asyncOperation.allowSceneActivation = false;
Debug.Log("Pro :" + asyncOperation.progress);
//When the load is still in progress, output the Text and progress bar
while (!asyncOperation.isDone)
{
//Output the current progress
m_Text.text = "Loading progress: " + (asyncOperation.progress * 100) + "%";
// Check if the load has finished
if (asyncOperation.progress >= 0.9f)
{
//Change the Text to show the Scene is ready
m_Text.text = "Press the space bar to continue";
//Wait to you press the space key to activate the Scene
if (Input.GetKeyDown(KeyCode.Space))
//Activate the Scene
asyncOperation.allowSceneActivation = true;
}
yield return null;
}
}
}
Source: AsyncOperation.progress
Everything else work fine. the scene load, it wait until is full loaded and proceed to the next scene. Even the debug update with the percent loaded. The only thing is not working is the text but thank you for your answer
Answer by misher · Apr 08, 2019 at 06:51 AM
try to put (operation.isDone == false)
instead of _progress < 1f
as a condition in your while loop
$$anonymous$$aking that change make the scene to not load
Your answer
Follow this Question
Related Questions
UI Text: Words at end of line jumping to next line 0 Answers
[HELP] To show wave number on a zombie survival game. 0 Answers
Changing the UI Text 0 Answers
Best way to attatch text to text script c# 1 Answer
Changing text occurs error: move initialization code to the Awake or Start function 0 Answers