Question by
pigment-TY · Jun 16, 2021 at 04:38 PM ·
time.deltatimeprogress bar
Progress bar doesn't work in the second scene
Hi~
I made two scenes and wrote a progress bar script for them as below. The script works fine in Unity Editor, yet doesn't work in the SECOND scene after built into an APK file, even though I swapped the two scenes (the first scene is always ok and the second one is not.)
However, if the second scene is reloaded (I made a "play again" button for reloading the scene), the progress bar works. I think it might be something lost when the first loading of the second scene but can't figure out which part gets wrong.
I'll appreciate the answer from anyone!
using UnityEngine;
using UnityEngine.UI;
public class TimeCountEvent : MonoBehaviour
{
public GameObject progressBar;
public Color[] progressBarColor = { Color.green, Color.yellow, Color.red };
public float totalTimeInSec;
public GameObject failMessage;
void Start()
{
failMessage.SetActive(false);
}
void Update()
{
ProgressBarRunning();
}
void ProgressBarRunning()
{
if (progressBar.transform.localScale.x > 0)
{
progressBar.transform.localScale -= new Vector3(Time.deltaTime / totalTimeInSec, 0, 0);
if (progressBar.transform.localScale.x > 0.66f)
progressBar.GetComponent<Image>().color = progressBarColor[0];
if (progressBar.transform.localScale.x > 0.33f && progressBar.transform.localScale.x <= 0.66f)
progressBar.GetComponent<Image>().color = progressBarColor[1];
if (progressBar.transform.localScale.x <= 0.33f)
progressBar.GetComponent<Image>().color = progressBarColor[2];
}
else
{
failMessage.SetActive(true);
progressBar.transform.localScale = new Vector3(0, 1, 1);
}
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613035109im_/https://answers.unity.com/themes/thub/images/avi.jpg)