- Home /
How do I obtain children on UI canvas text
I am still having a problem switching scenes so . I need to obtain the children that is with the ui text . Here is the script I have now :
sing UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class MyClockScript : MonoBehaviour
{
public int Minutes = 0;
public int Seconds = 0;
private Text m_text;
public float m_leftTime;
public void Awake()
{
m_text = GetComponent<Text>();
m_leftTime = GetInitialTime();
DontDestroyOnLoad(transform.gameObject);
}
public void Update()
{
if (m_leftTime > 0f)
{
// Update countdown clock
m_leftTime -= Time.deltaTime;
Minutes = GetLeftMinutes();
Seconds = GetLeftSeconds();
// Show current clock
if (m_leftTime > 0f)
{
m_text.text = "Time : " + Minutes + ":" + Seconds.ToString("00");
}
else
{
// The countdown clock has finished
m_text.text = "Time : 0:00";
}
}
}
private float GetInitialTime()
{
return Minutes * 60f + Seconds;
}
private int GetLeftMinutes()
{
return Mathf.FloorToInt(m_leftTime / 60f);
}
private int GetLeftSeconds()
{
return Mathf.FloorToInt(m_leftTime % 60f);
}
}
Comment
So, you need to get the GameObjects that are children of the GameObject that holds the Text component (referenced by m_text)? Just use GetComponentsInChildren and make it return RectTransform components since all UI GOs have a RectTransform component. Hope that helps. Be aware that GetComponentsInChildren also return the parent component if there is one.