- Home /
Integer turning to zero when I don't want
I made a counter that adds 1 to an integer every 60 seconds. I want that integer value to be added to another integer to be displayed on the scene after the level. For some reason, the original integer value keeps equaling zero when I reach that scene, even when it previously was 1.
Heres my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using UnityEngine.SceneManagement;
public class minutesscounter : MonoBehaviour
{
bool textupdate = true;
TMP_Text text2 = null;
public TMP_Text uiText2;
private bool isVisible = true;
TMP_Text text = null;
public TMP_Text uiText;
int minutes1 = 0;
int minutes = 0;
void Start()
{
text2 = uiText2.GetComponent<TMP_Text>();
text = uiText.GetComponent<TMP_Text>();
StartCoroutine(minutecounter());
}
IEnumerator minutecounter()
{
while (minutes < 100000) //Counts the minutes
{
yield return new WaitForSeconds(60);
minutes = minutes + 1;
}
}
void Update()
{
uiText.SetText(minutes + " minutes".ToString()); //Display on screen
if (Input.GetKeyDown(KeyCode.N))
{
if (isVisible)
{
HideUI(text);
isVisible = false;
}
else
{
ShowUI(text);
isVisible = true;
}
}
if (SceneManager.GetActiveScene () == SceneManager.GetSceneByName ("level1time")) //Hide this when the scene goes to "level1time"
{
if (Input.GetKeyDown(KeyCode.N))
{
HideUI(text);
}
if (isVisible)
{
HideUI(text);
}
if (textupdate == true)
{
Debug.Log(minutes1);
minutes1 = minutes;
uiText2.SetText(minutes1 + " minutes".ToString());
textupdate = false;
}
}
}
public static void HideUI(TMP_Text text) //Hide the UI on command
{
text.transform.localScale = new Vector3(0, 0, 0);
}
public static void ShowUI(TMP_Text text) //Re-show the UI on command
{
text.transform.localScale = new Vector3(1, 1, 1);
}
}
Any help is greatly appreciated :)
Answer by sSteinmann · May 05, 2021 at 06:54 AM
Have you set your GameObject as DontDestroyOnLoad which has the minutesscounter script on it? If not it will be destroyed during loading (if you unload the scene it's in) and so the counted minutes would be lost. You could also mark the minutes variable as static.
But why are you counting the minutes in a coroutine? You could simply save Time.time (float) on start and then at the display subtract the current time by the saved time, then you have the elapsed time in seconds, so you just have to divide it by 60 to get the minutes (you could also cast it to int if you don't care about partial minutes): elapsedMinutes = int(savedStartTime - Time.time)
You also don't need to check the scene name in update, you could subscribe to SceneManager.sceneLoaded or SceneManager.activeSceneChanged to get called when a scene is loaded or changed
@sSteinmann So turning the int $$anonymous$$utes to static fixed the problem of it not updating the text on the next scene. For some reason it now only adds 2 to $$anonymous$$utes every 2 $$anonymous$$utes instead of 1 to $$anonymous$$utes per $$anonymous$$ute.
That most likely means you have attached your script two times to some gameobject in your scene. This probably was your issue in the first place. So you may have referenced the wrong version originally? That's hatd to tell from what we know about your project.
Never$$anonymous$$d. The reason it would add more than necessary to the int was because I had multiple instances of the script running.