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. Dismiss Notice

[solved] access specific object from a group of similar named ones

Discussion in 'Scripting' started by ralf_b, Dec 9, 2017.

  1. ralf_b

    ralf_b

    Joined:
    Jul 9, 2013
    Posts:
    48
    Hello there,

    although this seems like a basic question I can not find any info on how to do this.
    I suppose because I even do not know WHAT words to google for :p

    What I have is several text objects hs_1_num ... hs_10_num, so now I want to change the color of one of these but have the number determined by another script.

    I have tried several variants to this:
    Code (CSharp):
    1.  
    2. hs_[new_number]_num.color = new Color(0.58f, 0f, 0f, 1f);
    3.  
    Hope you guys and gals do not laugh to hard at this :D and can offer some insight.
     
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
  3. ralf_b

    ralf_b

    Joined:
    Jul 9, 2013
    Posts:
    48
    I have used two dimensional arrays at max, only getting and setting values, so maybe I am missing something, but what?

    Let me rephrase the problem, I have 5 textfields:
    hs_1_num
    hs_2_num
    hs_3_num
    hs_4_num
    hs_5_num

    Now I want to randomly change the textcolor of one of those, and thought there must be a somewhat elegant way similar to this:
    Code (CSharp):
    1. new_number = Random.Range(1, 6);
    2. hs_[new_number]_num.color = new Color(0.58f, 0f, 0f, 1f);

    Instead of the brute force variant:
    Code (CSharp):
    1. new_number = Random.Range(1, 6);
    2. if (new_number == 1) {
    3.     hs_1_num.color = new Color(0.58f, 0f, 0f, 1f);
    4. } else if (new_number == 2) {
    5.     hs_2_num.color = new Color(0.58f, 0f, 0f, 1f);
    6. } else if (new_number == 3) {
    7.     hs_3_num.color = new Color(0.58f, 0f, 0f, 1f);
    8. } else if (new_number == 4) {
    9.     hs_4_num.color = new Color(0.58f, 0f, 0f, 1f);
    10. } else if (new_number == 5) {
    11.     hs_5_num.color = new Color(0.58f, 0f, 0f, 1f);
    12. }
    13.  
    Would you still say Arrays are the answer?
     
  4. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Code (csharp):
    1.  
    2.  
    3. Text[] texts = { hs_1, hs_2,...};
    4. texts[Random.Range(0,4)].color = new Color(...);
    5.  
    6.  
     
    ralf_b likes this.
  5. Fido789

    Fido789

    Joined:
    Feb 26, 2013
    Posts:
    343
    Well you can actually access your field using reflection, something like this:

    Code (CSharp):
    1. ((Text)GetType().GetField(String.Format("hs_{0}_num", Random.Range(0,4))).GetValue(this)).color = new Color(0.58f, 0f, 0f, 1f);
    But it will be slow as hell. And ugly. And stupid. Use fire7side's answer.
     
    ralf_b likes this.
  6. ralf_b

    ralf_b

    Joined:
    Jul 9, 2013
    Posts:
    48
    A great many thanks to fire7side for clearing that up :)
    Would never have thought about using an array that way.