Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Very Basic C# Question

Discussion in 'Scripting' started by McNoguff, Sep 29, 2015.

  1. McNoguff

    McNoguff

    Joined:
    Sep 20, 2015
    Posts:
    9
    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:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System;
    4.  
    5. public class CheckTimePM : MonoBehaviour {
    6.  
    7.     void FixedUpdate ()
    8.     {
    9.         if (DateTime.Now.ToString("tt") == "AM")
    10.         {
    11.             gameObject.SetActive (false);
    12.         }
    13.         else if (DateTime.Now.ToString("tt") == "PM")
    14.         {
    15.             gameObject.SetActive (true);
    16.         }
    17.     }
    18. }
    19.  
    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:
    Screen Shot 2015-09-29 at 4.34.41 PM.png
    AM looks like this:

    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:

    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.
     
  2. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    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.
     
    Last edited: Sep 29, 2015
    Kiwasi and McNoguff like this.
  3. McNoguff

    McNoguff

    Joined:
    Sep 20, 2015
    Posts:
    9
    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.
     
    Last edited: Sep 29, 2015
    Kiwasi likes this.