26.565
26.565

Reputation: 926

How can I access to UI Text?

I want to set my UI text from my script. I can access with public Text but I want to access with private game object.

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class WebsiteAd : MonoBehaviour
{
    private GameObject name;

    void Awake()
    {
        name = GameObject.Find("/Item0/Name");          
    }

    void Start()
    {
        name.GetComponent<Text>().text = "My Text";
    }

My Hierarchy Hierarchy

Upvotes: 2

Views: 321

Answers (1)

Mihai Dinculescu
Mihai Dinculescu

Reputation: 20033

GameObject.Find documentation states: "This function only returns active gameobjects."

In order to get access to inactive game objects, you need to use a function which supports them, like GameObject.GetComponentsInChildren.

public Component[] GetComponentsInChildren(Type type, bool includeInactive = false); 

Upvotes: 1

Related Questions