[C#] get an object in the scene

Hello everyones !

I’m working in a scene where there is a camera, a light and a canvas. The camera instanciate some cube in order to create a map when i launch the scene. The problem is : i want to acces to the canvas and be able to modify it in the script i put on my instantiated cube.
So far i’m using FindObjectOfType like this :

        Infos = new Text[8];

        ProtoUI = (Canvas) FindObjectOfType (typeof(Canvas));
        Infos = ProtoUI.GetComponents <Text> ();
        print (Infos.Length);

But there is not the result i want because when i print the size of Infos[ ] it says that it is 0…
How can i get the content of my canvas properly ?

You could try: GameObject.Find(“Canvas”).GetComponent().FindChild(“YOUR ELEMENT”).GetComponent();
Or simply adding a serialized field in your script and drag and drop your element in the inspector.

[SerializedField] Text myText;

GetComponentsInChildren is your friend as GetComponents does only search on the current object.

@panicq Too many string evaluations - not safe for refactoring and performance sensitive. :slight_smile:

@Glockenbeat good to know :wink:

Yeah, this is going to collapse into a steaming mess the moment you add a second canvas to the scene. And there are plenty of reasons to add a second canvas to the scene. While GameObject.Find has a bad rap, I’d suggest using it over relying on FindObjectOfType.

The other alternative is to use FindObjectsOfType and iterate over the results.

Thanx to the quick response guys !
I’m a bit lost with theses functions, i have search how to use them but i can’t do what i want with them, maybe i don’t use them properly …

The FindObjectOfType code :

        Infos = new Text[8];

        ProtoUI = FindObjectOfType <Canvas>();
        Infos = ProtoUI.GetComponents <Text> ();
        print (Infos.Length);
        Infos[0].text = posX + " / " + posY;

And the ComponentInChildren code :

        Infos = new Text[8];

        ProtoUI = GetComponentInChildren <Canvas>();
        Infos = ProtoUI.GetComponents <Text> ();
        print (Infos.Length);
        Infos [0].text = posX + " / " + posY;

In both case i got an error saying that Infos is empty .

Maybe if i post all my script it might be useful to understand.

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

public class CubeContent : MonoBehaviour {

    public    GameObject            Ore;

    private    Canvas                ProtoUI;
    private    Text[]                Infos;

    private GameObject            ContentOre;
    public    int                    posX;
    public    int                    posY;
    public    int                    EggContent;
    public    NinjaController[]    Ninjas;

    // Use this for initialization
    void Start () {
        EggContent = 0;
        Ninjas = null;
    }
   
    // Update is called once per frame
    void Update () {
        //appel des fonctions réseau.
    }
    void OnMouseDown(){
        print ("I've been clicked");
        Infos = new Text[8];

        ProtoUI = GetComponentInChildren <Can>();
        Infos = ProtoUI.GetComponents <Text> ();
        print (Infos.Length);
        Infos [0].text = posX + " / " + posY;
    } 

    public void    CreateOre(){
        int[] ValOre;
        ContentOre = (GameObject)Instantiate (Ore, new Vector3 (0, 0, 0), transform.rotation);

        ValOre = new int[7];
        ValOre [0] = 1;
        ValOre [1] = 1;
        ValOre [2] = 1;
        ValOre [3] = 1;
        ValOre [4] = 1;
        ValOre [5] = 1;
        ValOre [6] = 1;

        ContentOre.GetComponent<OreData>().setCoordinates (posX, 0, posY);
        ContentOre.GetComponent<OreData>().setOreAmount (ValOre);
        ContentOre.GetComponent<OreData>().setDisplayed ();
    }

    public    void    setPos(int x, int y){
        posX = x;
        posY = y;
        CreateOre ();
//        Instantiate (Cube, new Vector3 (x, 0, y), transform.rotation);
    }

    public    void    setEggValue(int Amount){
        EggContent += Amount;
        if (EggContent > 99)
            EggContent = 99;
        else
            EggContent = 0;
    }

    public    int    getposX(){
        return (posX);
    }

    public    int    getposY(){
        return (posY);
    }

    public    int    getEggVal(){
        return (EggContent);
    }

    public    int    getNinjaValue(int Number){
        if (Ninjas [Number])
            return (Ninjas [Number].getStatus());
        return (-1);
    }
}
    ProtoUI = FindObjectOfType<Canvas>();
    Infos = ProtoUI.GetComponentsInChildren <Text> ();
    print (Infos.Length);
1 Like

Wow thanks ! It works !