Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by MascarellDev · Oct 13, 2014 at 04:56 AM · guierrorbuttontextchange

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MascarellDev · Oct 13, 2014 at 07:14 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges