Forum rules - please read before posting.

Timer script help

edited February 2022 in Technical Q&A
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

  • edited February 2022
    So is there anyway to edit this script so you could set the start time through an event depending on the scene?

    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:

            using UnityEngine;
            using System.Collections;
            using UnityEngine.UI;
            using System;
            public class ClockExample : MonoBehaviour {
            public  Text textClock;
    
            private DateTime time;
            public  string hour = "12";
            public  string minute = "30";
    
            void Awake ()
            {
            time = DateTime.Now;
            textClock = GetComponent<Text>();
            }
            void Update (){
    
            //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');
            }
        } 
    

    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.

    using System.Collections;
    

    using System.Collections.Generic;
    using UnityEngine;

    public class MainScript : MonoBehaviour
    {

    ClockExample[] all_clocks;

    public string ClockHour = "15";
    public string ClockMinute = "54";

    // Start is called before the first frame update
    void Start()
    {
    Invoke("FindClocks", 0.5f);

    }

    void FindClocks()
    {
    all_clocks = FindObjectsOfType();
    }
    // Update is called once per frame
    void Update()
    {
    if (all_clocks != null && all_clocks.Length > 0)
    {
    foreach (ClockExample clock in all_clocks)
    {
    clock.hour = ClockHour;
    clock.minute = ClockMinute;
    }
    }

    }
    

    }

    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:

    AC.GVar stringVariable = AC.GlobalVariables.GetVariable ("ClockText");
    stringVariable.TextValue = ClockHour + ":" + ClockMinute;
    
  • edited February 2022

    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.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.