Search Unity

Question How can I scale by time a canvas scaler > scaler factor float value from 0.01 to 1 ?

Discussion in 'UGUI & TextMesh Pro' started by shamenraze1988, Feb 1, 2021.

  1. shamenraze1988

    shamenraze1988

    Joined:
    Nov 24, 2020
    Posts:
    208


    The minimum is 0.01f the maximum I want it to be is 1 I want that when I start a Coroutine to be able to decide the time it will take to change the value to 1 and also the direction if from 0.01 to 1 or from 1 to 0.01

    I tried this :

    Code (csharp):
    1.  
    2. if (buttonType == "LoadGame")
    3.        {
    4.            StartCoroutine(DisplaySavedGamesWindow(5f, true));
    5.        }
    6.  
    And

    Code (csharp):
    1.  
    2. IEnumerator DisplaySavedGamesWindow(float time, bool direction)
    3.    {
    4.        float scaler = GameObject.Find("Save Slots").GetComponent<CanvasScaler>().scaleFactor;
    5.  
    6.        float currentTime = 0;
    7.        while (currentTime < time)
    8.        {
    9.            scaler = currentTime / time;
    10.            currentTime += Time.deltaTime;
    11.  
    12.            yield return null;
    13.        }
    14.    }
    15.  
    I used a break point on the line :

    Code (csharp):
    1.  
    2. scaler = currentTime / time;
    3.  
    I see that scaler is changing up to 1 or to 0.9 ot sure why it's not getting to 1 And also it's not changing the value to 0.9 or to 1 in the editor in the scale factor it's still 0.01

    Also how to use the bool flag ? true should be from 0.01 to 1 and false from 1 to 0.01

    Ok I managed to make it working to be changed up from 0.01 to 1 :

    Code (csharp):
    1.  
    2. IEnumerator DisplaySavedGamesWindow(float time, bool direction)
    3.    {
    4.        float currentTime = 0;
    5.        while (currentTime < time)
    6.        {
    7.            var scaler = currentTime / time;
    8.            GameObject.Find("Save Slots").GetComponent<CanvasScaler>().scaleFactor = scaler;
    9.            currentTime += Time.deltaTime;
    10.  
    11.            yield return null;
    12.        }
    13.    }
    14.  
    The problem now is that when it's changing the value from the script the result is gray.

    This is how it looks like when I'm changing it to 1 in the editor : You can see the blue scroll view on the left :



    but when it's changing the value from the script the result is gray scroll view and not blue like before :