Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We’re making changes to the Unity Runtime Fee pricing policy that we announced on September 12th. Access our latest thread for more information!
    Dismiss Notice
  3. Dismiss Notice

[SOLVED]My UI text don't update from script

Discussion in 'UGUI & TextMesh Pro' started by DragOOneD999, Apr 7, 2019.

  1. DragOOneD999

    DragOOneD999

    Joined:
    Jan 10, 2017
    Posts:
    64
    I have the following script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.SceneManagement;
    6.  
    7. public class SceneFader : MonoBehaviour {
    8.  
    9.     public Image img;
    10.     public AnimationCurve curve;
    11.     private float _progress = 0f;
    12.     public Text loadingText;
    13.  
    14.     private void Start()
    15.     {
    16.         StartCoroutine(FadeIn());
    17.     }
    18.  
    19.     public void FadeTo(string scene)
    20.     {
    21.         StartCoroutine(FadeOut(scene));
    22.     }
    23.  
    24.     IEnumerator FadeIn()
    25.     {
    26.         float t = 1f;
    27.  
    28.         while(t > 0f)
    29.         {
    30.             t -= Time.deltaTime;
    31.             float a = curve.Evaluate(t);
    32.             img.color = new Color(0f, 0f, 0f, a);
    33.             yield return 0;
    34.         }
    35.     }
    36.  
    37.     IEnumerator FadeOut(string scene)
    38.     {
    39.         AsyncOperation operation = SceneManager.LoadSceneAsync(scene);
    40.  
    41.         operation.allowSceneActivation = false;
    42.  
    43.         while(_progress < 1f)
    44.         {
    45.             _progress = Mathf.Clamp01(operation.progress / 0.9f);
    46.  
    47.             loadingText.text = "Loading... " + (int)(_progress * 100f) + "%";
    48.  
    49.             //Debug.Log("Loading... " + (int)(_progress * 100f) + "%");
    50.  
    51.             yield return null;
    52.         }
    53.  
    54.         float t = 0f;
    55.  
    56.         while (t < 1f)
    57.         {
    58.             t += Time.deltaTime;
    59.             float a = curve.Evaluate(t);
    60.             img.color = new Color(0f, 0f, 0f, a);
    61.             loadingText.color = new Color(1f, 1f, 1f, a);
    62.             yield return 0;
    63.         }
    64.  
    65.         //SceneManager.LoadScene(scene);
    66.         operation.allowSceneActivation = true;
    67.     }
    68. }
    69.  
    and as the title say, the loadingText don't update to show what is in the function
     
  2. Discmage

    Discmage

    Joined:
    Aug 11, 2014
    Posts:
    57
    I don't know if this would help perhaps?

    Canvas.ForceUpdateCanvases();
     
  3. DragOOneD999

    DragOOneD999

    Joined:
    Jan 10, 2017
    Posts:
    64
    I put it like this
    Code (CSharp):
    1. private void OnEnable()
    2.     {
    3.         Canvas.ForceUpdateCanvases();
    4.     }
    and no results
     
  4. Discmage

    Discmage

    Joined:
    Aug 11, 2014
    Posts:
    57
    I think you have to force update AFTER you've edited the text though? if you have multiple changes you would have multiple updates? Just a random guess though :)
     
  5. DragOOneD999

    DragOOneD999

    Joined:
    Jan 10, 2017
    Posts:
    64
    still no changes
     
  6. DragOOneD999

    DragOOneD999

    Joined:
    Jan 10, 2017
    Posts:
    64
    It seems my text was not big enough to display all the content