Hi Team,
I have a UI I’m setting up that I want to programmatically change content. See attached picture illustrating the hierarchy of objects.
I have a script on the BackgroundPanel which searches for individual items, for example, the textbox “heading”, the textbox “LeftTextBox”, and the object “ObjectRight” which may have a video player, a render image, or some other content (currently to be defined, which will be created later).
I currently have the following code to find each item by name - which obviously is totally static. It works, but what I’d like to know is - is there a “best practice” or better way to do this?
public class UIContentController : MonoBehaviour
{
Text leftText;
Text linkBox;
GameObject objectRight;
GameObject heading;
// Use this for initialization
void Start () {
heading = transform.Find("Heading").gameObject;
leftText = transform.Find("LeftTextBox").GetComponent<Text>();
linkBox = transform.Find("GeniusLink").GetComponent<Text>();
objectRight = transform.Find("ObjectRight").gameObject;
}
}
If I use the code
leftText = GetComponentInChildren<Text>();
Then I find the first text component, without finding the correct text component.
Thanks in advance for any assistance.
,Hi Team,
I have a UI I’m setting up that I want to programmatically change content. See attached picture illustrating the hierarchy of objects.
I have a script on the BackgroundPanel which searches for individual items, for example, the textbox “heading”, the textbox “LeftTextBox”, and the object “ObjectRight” which may have a video player, a render image, or some other content (currently to be defined, which will be created later).
I currently have the following code to find each item by name - which obviously is totally static. It works, but what I’d like to know is - is there a “best practice” or better way to do this?
public class UIContentController : MonoBehaviour
{
Text leftText;
Text linkBox;
GameObject objectRight;
GameObject heading;
// Use this for initialization
void Start () {
heading = transform.Find("Heading").gameObject;
leftText = transform.Find("LeftTextBox").GetComponent<Text>();
linkBox = transform.Find("GeniusLink").GetComponent<Text>();
objectRight = transform.Find("ObjectRight").gameObject;
}
}
If I use the code
leftText = GetComponentInChildren<Text>();
Then I find the first text component, without finding the correct text component.
Thanks in advance for any assistance.