I am trying to make a simple background swap every 60 seconds. I try to do this by checking if a minute has passed with a timer variable. Is it because I am using a float for the timer, instead of an int? If so, how do I make this script work with an integer?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class DayNightCycle : MonoBehaviour
{
public TextMeshProUGUI text;
public GameObject daybg;
public GameObject nightbg;
private float timer;
void Start()
{
nightbg.SetActive(false);
}
void Update()
{
timer += Time.deltaTime;
if(timer % 60== 0)
{
nightbg.SetActive(true);
daybg.SetActive(false);
}
}
}
nightbg.SetActive(!nightbg.activeSelf); // invert the active state of the night bg
daybg.SetActive(!nightbg.activeSelf); // day should be on when night is off and vice versa
It does the same thing but may be a bit less confusing. Though a better solution would probably be to use an actual variable to track the day / night state