Very Basic C# Question

I’ve made it through a ton of tutorials(Unity’s community is awesome!) but I’m stuck on something that feels stupid obvious and basic. I’m starting the tutorials on Catlike coding and made the clock. I wanted a simple quad to show up with a moon or a sun based on AM/PM, and here’s the code:

using UnityEngine;
using System.Collections;
using System;

public class CheckTimePM : MonoBehaviour {

    void FixedUpdate ()
    {
        if (DateTime.Now.ToString("tt") == "AM")
        {
            gameObject.SetActive (false);
        }
        else if (DateTime.Now.ToString("tt") == "PM")
        {
            gameObject.SetActive (true);
        }
    }
}

And there’s an almost identical script attached to the AM object(public class CheckTimeAM), with the setactives reversed.

And if I open the app and it’s PM it looks like this:
2317804--156274--Screen Shot 2015-09-29 at 4.34.41 PM.png
AM looks like this:

2317804--156275--Screen Shot 2015-09-29 at 4.34.11 AM.png

It works! BUT if I have the app open already and change AM to PM or vice versa, it deactivates the active quad but does not activate the deactivated one. Like so:

2317804--156276--Screen Shot 2015-09-29 at 4.34.16 PM.png

If you see what’s going on in my code, let me know! I think I might have some simple fundamental misunderstanding of how SetActive works? Do I need to instantiate a prefab of the quads to do this? If so, why?

Thanks so much for your time folks! I tried searching for a reason for this but couldn’t find one, sorry if I missed an obvious resource.

Erm, it seems you deactivate the object that the script is attached to, so all the scripts won’t continue running. You have to activate and deactivate them from a script that is not attached to them.

2 Likes

Thank you! That was an incredibly stupid oversight on my part.

Edit: Fixed in mere moments. I will endeavor to post less stupid questions here on out.

1 Like