- Home /
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);
}
}
}
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);
}
}
}
}
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?
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?
Ok thanks. The last thing I am needing is for it to accept gameobjects not lights is this possable. Thanks for all your help.
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
Your answer
Follow this Question
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