The question is solved
Timer in UI panel does not reset
I have a script where, when an the player takes control of the final objective, the finalObjectiveTimer is supposed to start counting down. And it works. The panel appears and the timer counts down. When the player loses the objective and then retakes it again, the timer is supposed to restart. And it does - but only in the inspector. In the panel it continues where it left off. Any ideas why the one in the UI panel refuses to restart as well?
private void Update()
{
{
if (finalObjectiveTimerActivated == true)
{
timer = startTimer;
timer -= Time.deltaTime;
finalObjectiveTimer.text = timer.ToString("f0");
}
if (finalObjectiveTimerActivated == false)
{
timer = startTimer;
finalObjectiveTimer.text = timer.ToString("f0");
}
}
}
Answer by Eno-Khaon · May 07, 2018 at 06:07 PM
It feels like this shouldn't be the script where you're seeing this problem. In fact, I can't see how this script would actually "behave" in the inspector.
If the timer is activated, you set the time to the starting value, then decrease it by a fraction of a second. If, say, the timer is 10 seconds, that means the value should realistically never drop below about 9.98 (est. 50fps), since it would be reset to 10 every frame.
Are there other scripts you're trying to accomplish a similar goal in? I can't see this excerpt relating to the problem you're describing, unless there's further context that's missing.
Thanks for your answer - I did not expect something this fast :) You are right, it kept reseting but I didn't notice because I paid attention to the countdown on my UI panel. I corrected this script so it doesn't reset but now my UI timer stopped counting down. It appears with the coundtown value but doesn't count down while the timer in the inspector keeps counting down.
void Start()
{
objectiveTimer = maxObjectiveTimer;
player2ControlPanel.SetActive(true);
player1ControlPanel.SetActive(false);
}
private void Update()
{
if (changeOfControl == true)
{
objectiveTimer -= Time.deltaTime;
}
if (finalObjectiveTimerActivated == true)
{
finalObjectiveTimer = GetComponent<Text>();
timer -= Time.deltaTime;
finalObjectiveTimer.text = timer.ToString("f0");
if (timer <= 0)
{
Time.timeScale = 0;
missionSuccessPanel.SetActive(true);
}
}
if (finalObjectiveTimerActivated == false)
{
timer = startTimer;
finalObjectiveTimer.text = timer.ToString("f0");
}
}
void OnTriggerStay(Collider col)
{
{
if (col.gameObject.tag == "Player1")
if (objectiveTimer < 0)
{
{
player1Control = true;
player2Control = false;
changeOfControl = false;
objectiveTimer = maxObjectiveTimer;
if (finalObjective == true)
{
player1ControlPanel.SetActive(true);
player2ControlPanel.SetActive(false);
finalObjectiveTimerPanel.SetActive(true);
finalObjectiveTimerActivated = true;
timer = startTimer;
}
}
}`
private void OnTriggerEnter(Collider col)
{
if (col.gameObject.tag == "Player1" || col.gameObject.tag == "Player2")
changeOfControl = true;
}
`
I'd be rather surprised if you aren't getting Null Reference Exceptions in this case. From what I can tell, the problem primarily lies in this line:
finalObjectiveTimer = GetComponent<Text>();
You appear to have a variable set aside for finalObjectiveTimerPanel as a GameObject (based on its usage), so you should presumably be calling GetComponent on it, rather than on the currently object, which I assume is a separate Game Controller and not the Timer object itself.
That said, based on what you've provided, it may make more sense to make that GetComponent<>() call when you switch over to the "final objective" portion (currently in OnTriggerStay()), so you're not calling that function every frame for the same object.
Problem solved. You were right about the finalObjectiveTimer = GetComponent(); line being the problem. As soon as I commented it out the timer started to work perfectly.
Hi, thanks for your answer.
I moved the GetComponent<>() call as per your suggestion but I'm having a hard time understanding what you mean in that first paragraph - I'm at a very basic level in this stuff. Are you saying that the script that runs the timer should be attached to that timer panel ins$$anonymous$$d to the "FinalObjective" itself?
There's no need for that if you have a reference to that object. As long as the GameObject is assigned properly, you can get a component from another one easily enough.
finalObjectiveTimer = finalObjectiveTimerPanel.GetComponent<Text>();