Any idea how I could set this clock to start at a particular time on scene start?
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System;
public class ClockExample : MonoBehaviour {
private Text textClock;
void Awake (){
textClock = GetComponent<Text>();
}
void Update (){
DateTime time = DateTime.Now;
string hour = LeadingZero( time.Hour );
string minute = LeadingZero( time.Minute );
// string second = LeadingZero( time.Second );
textClock.text = hour + ":" + minute ;//+ ":" + second;
}
string LeadingZero (int n){
return n.ToString().PadLeft(2, '0');
}
}
Comments
I know it’s not specifically AC related but might be interesting for the forum?
You're reading DateTime.Now, which will always be the current time rather than a specific time.
AC is not involved here, however - see Unity's forums for advice on this topic.
Morning, I now need help how to make my scripts wirj with AC, so I thik this is now a relevant AC thread (I hope).
So now I have written two scripts. I have adapted the ClockExample script to set a start time:
And as my timer is in a unity ui prefab menu, I try to access it from a scene, with this script attached to a GameObject where you can set the time for the scene.
However, It is not working! Is there a way to make this a Custom Action instead somehow?
See screenshots also. thanks:
https://www.dropbox.com/sh/xg8xjjoi0vuwzfa/AABqUxkt0KM-kezZfQMMUkZ2a?dl=0
If the Menu that contains the ClockExample script is not turned on at the time that "FindClocks" is called, then it will not be found and included in the all_clocks array.
If the clock is represented by a Text label, it's better to rely on a Global String variable (named e.g. "ClockText") that stores the text, and then display that text inside Label menu elements by setting the Label type field to Global Variable.
This code can be used to update the Global String variable's value:
And sorry, what would I wrap the above code around with?
See screenshot for Global V:
https://www.dropbox.com/s/qsdb0m3qajadho2/Timer - Global V.png?dl=0
You would run that code whenever you want to update the variable. To update it every frame, for example, have it replace the contents of your MainScript's Update function.