How to check if a minute has passed?

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);
        }
       
    }
}

Yes your timer will never exactly equal 60 or multiples thereof. You should not use an integer. You should do something like this:

float interval = 60;
void Update()
{
    timer += Time.deltaTime;
    if(timer >= interval)
    {
        timer -= interval;
        nightbg.SetActive(true);
        daybg.SetActive(false);
    }
}
2 Likes

Great, now how would I have these two backgrounds switch between each other every minute? I’m new and this is my best guess. It doesn’t work though.

void Update()
    {
        timer += Time.deltaTime;
       
        if(timer >= interval)
        {
            timer -= interval;
            nightbg.SetActive(true);
            daybg.SetActive(false);
        }
        if (timer >= interval)
        {
            timer -= interval;
            nightbg.SetActive(false);
            daybg.SetActive(true);
        }
    }

?

Something like this would do:

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
1 Like

Thanks you very much! This makes sense. :slight_smile:

Uhh, double negation is confusing :slight_smile: I would base the day / night state on the day background and would switch them like this:

nightbg.SetActive(daybg.activeSelf);
daybg.SetActive(!daybg.activeSelf);

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

private bool isDay = true;
// [ ... ]

isDay = !isDay;

daybg.SetActive(isDay);
nightbg.SetActive(!isDay);

This could even be packed into a property so when you set the IsDay property, you change the backgrounds accordingly

private bool m_IsDay;
public bool IsDay
{
    get => m_IsDay;
    set
    {
        m_IsDay = value;
        daybg.SetActive(m_IsDay);
        nightbg.SetActive(!m_IsDay);
    }
}

Now you can simply do

IsDay = !IsDay;

to toggle the day / night state or set it to true / false explicitly and the backgrounds would change as well.

Thanks for the info. I’m glad there is more than one way to do things when programming. Code being flexible is nice.