Search Unity

For Loop, Find Objects, Access UI Text

Discussion in 'Scripting' started by kodagames, Jun 22, 2018.

  1. kodagames

    kodagames

    Joined:
    Jul 8, 2009
    Posts:
    548
    Hello,

    Im trying to go through the for loop and find all of the game objects who's name begins with HC (there are HC1, HC2, etc). Once those objects are found, I want to give each game object (beginning with HC) a card from my list (array) everything is working except this part and Im stuck...

    Things that are not working:
    Code (CSharp):
    1. void giveCards()
    2.     {
    3.         for (int i = 0; i <= 8; i++)
    4.         {
    5.  
    6.             if (i <= 1)
    7.             {
    8.  
    9.                 GameObject PlayerCards = GameObject.Find ("HC " + (i + 1));
    10.                 PlayerCards.GetComponent(Text).text = cards [i];
    11.  
    12.             }
    13.         }
    14.         Debug.Log ("cards should arrive");
    15.     }
    16. }
    or

    Code (CSharp):
    1. void giveCards()
    2.     {
    3.         for (int i = 0; i <= 8; i++)
    4.         {
    5.  
    6.             if (i <= 1)
    7.             {
    8.                
    9.                 Text something = GameObject.Find("HC " + (i + 1)).GetComponent<Text>();
    10.                 something = cards [i];
    11.  
    12.  
    13.             }
    14.         }
    15.         Debug.Log ("cards should arrive");
    16.     }
    Oh.. the text is a UI text which is a child of an object in the Hierarchy.
    Any help is appreciated!
    An explanation of why it work would be terrific if you have time ;)
     
    Last edited: Jun 22, 2018
  2. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    Assuming cards is an array of strings,
    The first example doesn't work because you are using GetComponent incorrectly. Put the type in angular brackets:

    GetComponent<Text>().text = cards;

    The second one doesn't work as you cannot assign a string to a variable of type Text, they are different classes.
     
    kodagames likes this.
  3. kodagames

    kodagames

    Joined:
    Jul 8, 2009
    Posts:
    548
    @bobisgod234

    Appreciate the help. Yes cards is an array of strings.

    Code (CSharp):
    1. public int numberOfCards = 52;
    2.     public string[] cards;
    When I try:
    Code (CSharp):
    1. void giveCards()
    2.     {
    3.         for (int i = 0; i <= 8; i++)
    4.         {
    5.  
    6.             if (i <= 1)
    7.             {
    8.                 GameObject PlayerCards = GameObject.Find ("HC " + (i + 1));
    9.                 PlayerCards.GetComponent<Text>.text = cards;
    10.             }
    11.         }
    12.         Debug.Log ("cards should arrive");
    13.     }
    The error I get is:
    Assets/CardManger.cs(111,17): error CS0119: Expression denotes a `method group', where a `variable', `value' or `type' was expected

    When using the older version of unity with guiText this used to work but I can't seem to figure it out with the newer version of unity (the new UI system):
    Code (CSharp):
    1. void giveCards()
    2.     {
    3.         for (int i = 0; i <= 8; i++)
    4.         {
    5.  
    6.             if (i <= 1)
    7.             {
    8.                 GameObject PlayerCards = GameObject.Find ("HC " + (i + 1));
    9.                 PlayerCards.guiText.text = cards [i];
    10.             }
    11.         }
    12.         Debug.Log ("cards should arrive");
    13.     }
     
  4. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    You missed the ()'s this time.

    You want this:

    PlayerCards.GetComponent<Text>().text = cards;

    note the type in the < and >'s, and also the ()'s
     
    kodagames likes this.
  5. kodagames

    kodagames

    Joined:
    Jul 8, 2009
    Posts:
    548
    @bobisgod234
    Doh... can't believe I forgot the brackets that time.

    Thank you Sooooooooo Much!!! :):):D:):) it helped me really find the issue ;) I'm amazed this thing is working Wooohooo!

    PlayerCards.GetComponent<Text>().text = cards;