timeScale is being set to 1 instead of staying set at 0,How to fix the timeScale?
I'm having a problem with the script above. The problem is that when the script is run (the script is on a separate canvas), the timeScale of the canvas that is being overlapped by the script canvas' timeScale is being set to 1, when the timeScale should be set to 0. In short, the background of the canvas where the script is, is setting its own timeScale to 1 instead of setting and keeping the timeScale at 0, where it should be when the script canvas is called...
Anyone have any ideas how to fix this?,
I'm having a problem with this code that I can't figure out. The problem is when I run the scene in Unity, once the script above is called (the script itself is on another canvas), the canvas in the background's timeScale gets set to 1. The canvas in the background's timeScale is supposed to be set to 0 when the canvas where the script is on is opened, but the timer sets the timescale back to 1...
Anyone have any ideas on how to possibly fix this?
i dont really understand your issue, you mean that the timescale of one canvas is 0 and the other is 1? the timescale is being set back to 1because i imagine by opened you mean active and you are setting the timescale to 1 in the start
Answer by oldnavy281 · May 30, 2019 at 01:56 PM
so there is an object in the game that once destroyed sets time scale to 0. And then a canvas appears that has a timer. Since the time scale is set to 0 the timer doesn't work, but the time scale needs to be set to 0 for other aspects of the game. Is there a way to allow the timer to has timescale of 1 for example, while the rest of the game is set to timescale of 0?
Answer by xxmariofer · May 30, 2019 at 02:12 PM
Here you have the code just override your coroutine
IEnumerator LoseTime()
{
while (timeLeft> 0) {
yield return null;
timeLeft -= Time.unscaledDeltaTime;
}
}
I received an error after I edited the code. This is the error:
Assets/$$anonymous$$ath Blasters/Assets/Start $$anonymous$$enu/Timer.cs(26,21): error CS0266: Cannot implicitly convert type 'float' to 'int'. An explicit conversion exists (are you missing a cast?)
The updated code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Threading;
public class Timer : $$anonymous$$onoBehaviour {
public int timeLeft = 30; //Seconds Overall
public Text countdown; //UI Text Object
void Start () {
StartCoroutine("LoseTime");
Time.timeScale = 1; //Just making sure that the timeScale is right
}
void Update () {
countdown.text = ("" + timeLeft); //Showing the Score on the Canvas
}
//Simple Coroutine
IEnumerator LoseTime()
{
while (timeLeft > 0) {
yield return null;
timeLeft -= Time.unscaledDeltaTime;
}
}}
change public int timeLeft to public float timeLeft
Your answer
Follow this Question
Related Questions
Menu object not responding 0 Answers
Unity {[(2D)]} RotateAround while jumping 0 Answers
How to make a boat move without controling it ? 0 Answers
Auto Set vs Manual Decrease (How to do both?) 1 Answer
How can I fix this animation flick 0 Answers