- Home /
Change text gui.button in a for bucle
Hello! first of all sorry for my english, is not my native language.
Im making a small memory game and im getting problems when i make the buttons. The problem is that i cannot change the text in the buttons and i want them empty and when you press one of them, show the text
 using UnityEngine;
 using System.Collections;
 
 public class CardsManager : MonoBehaviour 
 {
     private string[] cards;
     private string[] romaji; // romaji characters
     private string[] hiragana; // hiragana characters
     private string[] katakana; //katakana characters
 
     //The cards for the game
     float card1;
     float card2;
     float card3;
     float card4;
     float card5;
     float card6;
     float card7;
     float card8;
 
     void Start () 
     {
 
                 //Romaji characters
                 romaji = new string[] {
             "a", "i", "u", "e", "o", };
 
                 //Hiragana characters
                 hiragana = new string[] {
             "あ", "い", "う", "え", "お", };
 
         GenerateCards ();
 
     }
 
     void Update ()
     {
 
     }
 
     void OnGUI () 
     {
         //Make the gui of 480*800
         float scaleX = (float)(Screen.width) / 480.0f;
         float scaleY = (float)(Screen.height) / 800.0f;
         GUI.matrix = Matrix4x4.TRS(new Vector3(0, 0, 0), Quaternion.identity, new Vector3(scaleX, scaleY, 1));
         
         int temp = 0;
         string empty = "";
         
         for(int x = 0; x <= 3; x++)
         {
             for(int y = 0; y <= 3; y++)
             {
                 if(GUI.Button(new Rect(25 + (110 * x),250 + (110 * y), 100, 100), "" + cards[temp++]))
                 {
 
                 }
             }
         }
     }
 
     void GenerateCards ()
     {
         card1 = Random.Range (0, 113);
         card2 = Random.Range (0, 113);
         card3 = Random.Range (0, 113);
         card4 = Random.Range (0, 113);
         card5 = Random.Range (0, 113);
         card6 = Random.Range (0, 113);
         card7 = Random.Range (0, 113);
         card8 = Random.Range (0, 113);
 
         cards = new string[] {
             hiragana[(int)card1], 
             hiragana[(int)card2], 
             hiragana[(int)card3], 
             hiragana[(int)card4], 
             hiragana[(int)card5],
             hiragana[(int)card6],
             hiragana[(int)card7],
             hiragana[(int)card8],
             romaji[(int)card1],
             romaji[(int)card2], 
             romaji[(int)card3],
             romaji[(int)card4],
             romaji[(int)card5],
             romaji[(int)card6],
             romaji[(int)card7],
             romaji[(int)card8]};
         
         //Knuth shuffle algorithm
         for (int x= 0; x < 15; x++)
         {
             string temp = cards[x];
             int random = Random.Range(x, 15);
             cards[x] = cards[random];
             cards[random] = temp;
         }
     }
 }
well, the strings are much longer but i dont consider relevant put every character inside of them here. I hope someone can help me or show me some links where i can find a solution.
Also im getting the error NullReferenceException: Object reference not set to an instance of an object and i dooooooont really know why, if someone can help with this too i'll be grateful.
Answer by zharik86 · Oct 13, 2014 at 06:25 AM
First, about null reference exception. Your arrays "romaji" and "hiragama" have length 5, but you try access in function GenerateCards() at index much more 5. Random.Range(0, 113) return int value from 0 to 112. Second, as I understand, text of button is not visible before button is pressed. In your case, create array for showing text:
  private bool[] cardsShow = null; //create boolean array for showing cards
  void GenerateCards () {
   //Simple way, not use all variables card1 and etc, create loop
   cards = new string[16]; //count 16 from your code
   cardsShow = new bool[16]; //allocate our array
   for(int i = 0; i < cards.Length/2; i++) {
    card1 = Random.Range (0, hiragana.Length); //not 113, length hiragana and romaji shall be match
    cards[i] = hiragana[card1];
    cards[i + cards.Length/2] = romaji[card1];
    //Initialization bool array
    cardsShow[i] = false;
    cardsShow[i + cards.Length/2] = false;
   }
   //Knuth shuffle algorithm
   for (int x= 0; x < 15; x++) { //maybe replace "15" on "cards.Length-1"?
    string temp = cards[x];
    int random = Random.Range(x, 15);
    cards[x] = cards[random];
    cards[random] = temp;
   }
  }
  void OnGUI () {
   //Make the gui of 480*800
   float scaleX = (float)(Screen.width) / 480.0f;
   float scaleY = (float)(Screen.height) / 800.0f;
   GUI.matrix = Matrix4x4.TRS(new Vector3(0, 0, 0), Quaternion.identity, new Vector3(scaleX, scaleY, 1));
 
   //int temp = 0; //no need to use this variable
   string empty = "";
   for(int x = 0; x <= 3; x++) {
    for(int y = 0; y <= 3; y++) {
     //Use cardsShow for showing text
     if(GUI.Button(new Rect(25 + (110 * x),250 + (110 * y), 100, 100), (cardsShow[4*x+y]?cards[4*x+y]:""))) {
      cardsShow[4*x+y] = true;
     }
    }
   }
  }
I hope that it will help you.
Nooo, the arrays hiragana and romaji has 114 characters each one, but i dont put everything here.
And the code work perfectly, also i change all the numbers for x.Lenght , thank you!
Your answer
 
 
             Follow this Question
Related Questions
Change Text of GUI Button from Script 2 Answers
How do I set a GUI button's text using a string from another script? 0 Answers
Find GUI Button and Assign Text 1 Answer
Bool script error 1 Answer
uGUI Button size based on text size 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                