Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by davidflynn2 · Nov 27, 2012 at 06:44 PM · c#timedatecleanup

Cleaning this code up.

I have setup the fallowing code so that on a specfic time and date it turns my game objects on and off. I realy like how it works but I am trying to setup so many items in it that you end up with a mile long list of areas to drop the game objects in the inspecter. I know an array would help this out majorly but I have never made one so I am not sure how to go about this if some one could lend me a hand I would be most appresheateive.

There may be a way to collaps the list in the inspector that would work to if any one knows a way I could do this.

This is my code:

 using UnityEngine;
 using System.Collections;
 
 public class CheckDate : MonoBehaviour 
     {
     
     public string theTime = System.DateTime.Now.ToString("hh:mm:ss"); 
     public string theDate = System.DateTime.Now.ToString("MM/dd/yyyy"); 
     
     
     
     public string StartDate = "mm/dd/yyy";
     public string StartTime = "hh:mm:ss";
     
     public string EndDate = "mm/dd/yyy";
     public string EndTime = "hh:mm:ss";
     
     public GameObject lightSet1;
     public GameObject lightSet2;
     public GameObject lightSet3;
     public GameObject lightSet4;
     public GameObject lightSet5;
     public GameObject lightSet6;
     public GameObject lightSet7;
     public GameObject lightSet8;
     public GameObject lightSet9;
     public GameObject lightSet10;
     public GameObject lightSet11;
     public GameObject lightSet12;
     public GameObject lightSet13;
     public GameObject lightSet14;
     public GameObject lightSet15;
     public GameObject lightSet16;
     public GameObject lightSet17;
     public GameObject lightSet18;
     public GameObject lightSet19;
     public GameObject lightSet20;
     public GameObject lightSet21;
     public GameObject lightSet22;
     public GameObject lightSet23;
     public GameObject lightSet24;
     public GameObject lightSet25;
     public GameObject lightSet26;
     public GameObject lightSet27;
     public GameObject lightSet28;
     public GameObject lightSet29;
     public GameObject lightSet30;
     public GameObject lightSet31;
     public GameObject lightSet32;
     
     
     
     
 
     
     
     
 
 
     // Use this for initialization
     void Start () 
     {
     
     }
     
     // Update is called once per frame
     void Update () 
     {
     theTime = System.DateTime.Now.ToString("hh:mm:ss"); 
     theDate = System.DateTime.Now.ToString("MM/dd/yyyy"); 
         
         if(theDate == (StartDate) && theTime == (StartTime))
         {
             lightSet1.SetActiveRecursively(false);
             lightSet2.SetActiveRecursively(false);
             lightSet3.SetActiveRecursively(false);
             lightSet4.SetActiveRecursively(false);
             lightSet5.SetActiveRecursively(false);
             lightSet6.SetActiveRecursively(false);
             lightSet7.SetActiveRecursively(false);
             lightSet8.SetActiveRecursively(false);
             lightSet9.SetActiveRecursively(false);
             lightSet10.SetActiveRecursively(false);
             lightSet11.SetActiveRecursively(false);
             lightSet12.SetActiveRecursively(false);
             lightSet13.SetActiveRecursively(false);
             lightSet14.SetActiveRecursively(false);
             lightSet15.SetActiveRecursively(false);
             lightSet16.SetActiveRecursively(false);
             lightSet17.SetActiveRecursively(false);
             lightSet18.SetActiveRecursively(false);
             lightSet19.SetActiveRecursively(false);
             lightSet20.SetActiveRecursively(false);
             lightSet21.SetActiveRecursively(false);
             lightSet22.SetActiveRecursively(false);
             lightSet23.SetActiveRecursively(false);
             lightSet24.SetActiveRecursively(false);
             lightSet25.SetActiveRecursively(false);
             lightSet26.SetActiveRecursively(false);
             lightSet27.SetActiveRecursively(false);
             lightSet28.SetActiveRecursively(false);
             lightSet29.SetActiveRecursively(false);
             lightSet30.SetActiveRecursively(false);
             lightSet31.SetActiveRecursively(false);
             lightSet32.SetActiveRecursively(false);
             
             
         }
         
         if(theDate == (EndDate) && theTime == (EndTime))
         {
                 lightSet1.SetActiveRecursively(true);
             lightSet2.SetActiveRecursively(true);
             lightSet3.SetActiveRecursively(true);
             lightSet4.SetActiveRecursively(true);
             lightSet5.SetActiveRecursively(true);
             lightSet6.SetActiveRecursively(true);
             lightSet7.SetActiveRecursively(true);
             lightSet8.SetActiveRecursively(true);
             lightSet9.SetActiveRecursively(true);
             lightSet10.SetActiveRecursively(true);
             lightSet11.SetActiveRecursively(true);
             lightSet12.SetActiveRecursively(true);
             lightSet13.SetActiveRecursively(true);
             lightSet14.SetActiveRecursively(true);
             lightSet15.SetActiveRecursively(true);
             lightSet16.SetActiveRecursively(true);
             lightSet17.SetActiveRecursively(true);
             lightSet18.SetActiveRecursively(true);
             lightSet19.SetActiveRecursively(true);
             lightSet20.SetActiveRecursively(true);
             lightSet21.SetActiveRecursively(true);
             lightSet22.SetActiveRecursively(true);
             lightSet23.SetActiveRecursively(true);
             lightSet24.SetActiveRecursively(true);
             lightSet25.SetActiveRecursively(true);
             lightSet26.SetActiveRecursively(true);
             lightSet27.SetActiveRecursively(true);
             lightSet28.SetActiveRecursively(true);
             lightSet29.SetActiveRecursively(true);
             lightSet30.SetActiveRecursively(true);
             lightSet31.SetActiveRecursively(true);
             lightSet32.SetActiveRecursively(true);
             
         }
     }
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Landern · Nov 27, 2012 at 06:48 PM

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class CheckDate : MonoBehaviour 
     {
 
     public string theTime = System.DateTime.Now.ToString("hh:mm:ss"); 
     public string theDate = System.DateTime.Now.ToString("MM/dd/yyyy"); 
 
     public string StartDate = "mm/dd/yyy";
     public string StartTime = "hh:mm:ss";
 
     public string EndDate = "mm/dd/yyy";
     public string EndTime = "hh:mm:ss";
 
     public List<GameObject> lights = new List<GameObject>();
 
     // Use this for initialization
     void Start () 
     {
     }
 
     // Update is called once per frame
     void Update () 
     {
     theTime = System.DateTime.Now.ToString("hh:mm:ss"); 
     theDate = System.DateTime.Now.ToString("MM/dd/yyyy"); 
 
        if(theDate == (StartDate) && theTime == (StartTime))
        {
          foreach(GameObject light in lights)
          {
             light.SetActiveRecursively(false);
          }
        }
 
        if(theDate == (EndDate) && theTime == (EndTime))
        {
          foreach(GameObject light in lights)
          {
             light.SetActiveRecursively(true);
          }
        }
     }
 }
Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image davidflynn2 · Nov 27, 2012 at 06:50 PM 0
Share

I get the error : (16,12): error CS0246: The type or namespace name `List`1' could not be found. Are you missing a using directive or an assembly reference?

avatar image Landern · Nov 27, 2012 at 06:51 PM 1
Share

Opps, needed namespace System.Collections.Generic

avatar image davidflynn2 · Nov 27, 2012 at 06:53 PM 0
Share

Interesting now I got another error : error CS0246: The type or namespace name `GameObjects' could not be found. Are you missing a using directive or an assembly reference?

avatar image Landern · Nov 27, 2012 at 07:02 PM 1
Share

Typo, remove the 's', i'll fix example.

avatar image davidflynn2 · Nov 27, 2012 at 07:06 PM 0
Share

Ok thanks. The last thing I am needing is for it to accept gameobjects not lights is this possable. Thanks for all your help.

Show more comments
avatar image
1

Answer by Bluestrike · Nov 27, 2012 at 06:54 PM

You can create an array so when the array is collapsed you can see other settings in the inspector. It also makes it easyer to modify the objects. I am used to javascript so here is how that looks.

 // Create a array accesable in the inspector where you can assign al the gameobjects.
 var lightSet :GameObject[];
 
 
 function Update()
 {
     if(condition you need for when its true)
     {
         SetActive(true);
     }
     else if(condition you need for when its false)
     {
         //Disable again
         SetActive(false);
     }
 }
 
 //Function to enable/disable the gameobject.
 function SetActive(active :boolean) :void
 {
     for(light :gameObject in lightSet)
     {
         // unity 3.x
         light.SetActiveRecursively(active);
         // unity 4.x 
         light.SetActive(active);
     }
 
 }

I find this usefull for working with arrays and list:

http://wiki.unity3d.com/index.php?title=Which_Kind_Of_Array_Or_Collection_Should_I_Use%3F

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Get System date 1 Answer

C# Make something happen for X amount of Seconds. 2 Answers

Find time value between two other time values? C# 2 Answers

Pause Game When Function Is Enabled? 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges