Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[C#] get an object in the scene

Discussion in 'Scripting' started by _HAL_9000, Jun 29, 2015.

  1. _HAL_9000

    _HAL_9000

    Joined:
    Jun 26, 2015
    Posts:
    48
    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 :

    Code (CSharp):
    1.         Infos = new Text[8];
    2.  
    3.         ProtoUI = (Canvas) FindObjectOfType (typeof(Canvas));
    4.         Infos = ProtoUI.GetComponents <Text> ();
    5.         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 ?
     
  2. panicq

    panicq

    Joined:
    Sep 29, 2012
    Posts:
    37
    You could try: GameObject.Find("Canvas").GetComponent<Transform>().FindChild("YOUR ELEMENT").GetComponent<Text>();
    Or simply adding a serialized field in your script and drag and drop your element in the inspector.

    [SerializedField] Text myText;
     
  3. Glockenbeat

    Glockenbeat

    Joined:
    Apr 24, 2012
    Posts:
    669
    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. :)
     
  4. panicq

    panicq

    Joined:
    Sep 29, 2012
    Posts:
    37
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    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.
     
  6. _HAL_9000

    _HAL_9000

    Joined:
    Jun 26, 2015
    Posts:
    48
    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 :

    Code (CSharp):
    1.         Infos = new Text[8];
    2.  
    3.         ProtoUI = FindObjectOfType <Canvas>();
    4.         Infos = ProtoUI.GetComponents <Text> ();
    5.         print (Infos.Length);
    6.         Infos[0].text = posX + " / " + posY;
    And the ComponentInChildren code :

    Code (CSharp):
    1.         Infos = new Text[8];
    2.  
    3.         ProtoUI = GetComponentInChildren <Canvas>();
    4.         Infos = ProtoUI.GetComponents <Text> ();
    5.         print (Infos.Length);
    6.         Infos [0].text = posX + " / " + posY;
    In both case i got an error saying that Infos is empty .
     
  7. _HAL_9000

    _HAL_9000

    Joined:
    Jun 26, 2015
    Posts:
    48
    Maybe if i post all my script it might be useful to understand.

    Code (CSharp):
    1. sing UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class CubeContent : MonoBehaviour {
    6.  
    7.     public    GameObject            Ore;
    8.  
    9.     private    Canvas                ProtoUI;
    10.     private    Text[]                Infos;
    11.  
    12.     private GameObject            ContentOre;
    13.     public    int                    posX;
    14.     public    int                    posY;
    15.     public    int                    EggContent;
    16.     public    NinjaController[]    Ninjas;
    17.  
    18.     // Use this for initialization
    19.     void Start () {
    20.         EggContent = 0;
    21.         Ninjas = null;
    22.     }
    23.    
    24.     // Update is called once per frame
    25.     void Update () {
    26.         //appel des fonctions réseau.
    27.     }
    28.     void OnMouseDown(){
    29.         print ("I've been clicked");
    30.         Infos = new Text[8];
    31.  
    32.         ProtoUI = GetComponentInChildren <Can>();
    33.         Infos = ProtoUI.GetComponents <Text> ();
    34.         print (Infos.Length);
    35.         Infos [0].text = posX + " / " + posY;
    36.     }
    37.  
    38.     public void    CreateOre(){
    39.         int[] ValOre;
    40.         ContentOre = (GameObject)Instantiate (Ore, new Vector3 (0, 0, 0), transform.rotation);
    41.  
    42.         ValOre = new int[7];
    43.         ValOre [0] = 1;
    44.         ValOre [1] = 1;
    45.         ValOre [2] = 1;
    46.         ValOre [3] = 1;
    47.         ValOre [4] = 1;
    48.         ValOre [5] = 1;
    49.         ValOre [6] = 1;
    50.  
    51.         ContentOre.GetComponent<OreData>().setCoordinates (posX, 0, posY);
    52.         ContentOre.GetComponent<OreData>().setOreAmount (ValOre);
    53.         ContentOre.GetComponent<OreData>().setDisplayed ();
    54.     }
    55.  
    56.     public    void    setPos(int x, int y){
    57.         posX = x;
    58.         posY = y;
    59.         CreateOre ();
    60. //        Instantiate (Cube, new Vector3 (x, 0, y), transform.rotation);
    61.     }
    62.  
    63.     public    void    setEggValue(int Amount){
    64.         EggContent += Amount;
    65.         if (EggContent > 99)
    66.             EggContent = 99;
    67.         else
    68.             EggContent = 0;
    69.     }
    70.  
    71.     public    int    getposX(){
    72.         return (posX);
    73.     }
    74.  
    75.     public    int    getposY(){
    76.         return (posY);
    77.     }
    78.  
    79.     public    int    getEggVal(){
    80.         return (EggContent);
    81.     }
    82.  
    83.     public    int    getNinjaValue(int Number){
    84.         if (Ninjas [Number])
    85.             return (Ninjas [Number].getStatus());
    86.         return (-1);
    87.     }
    88. }
     
  8. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Code (CSharp):
    1.     ProtoUI = FindObjectOfType<Canvas>();
    2.     Infos = ProtoUI.GetComponentsInChildren <Text> ();
    3.     print (Infos.Length);
     
    _HAL_9000 likes this.
  9. _HAL_9000

    _HAL_9000

    Joined:
    Jun 26, 2015
    Posts:
    48
    Wow thanks ! It works !