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
1
Question by blutwurstchen · Dec 27, 2011 at 01:58 AM · guibuttonarraycharacterstring

changing GUI Button text with a string array

I am working on a character selection screen.

Thought it would save me some time to store different button states in an array (which could also be sent to a playerStatus script. I plan for the text in the button to change with each click. A material changing script will access this 'On GUI' and change the character's materials appropriately.

I must have the syntax wrong, because Unity is telling me that the 'System.Type' does not support slicing'.

I have searched in vain for somebody else trying to do this, can anyone see what I am doing wrong?

Thanks in advance for any help.

 var eyesColour = String["Blue","Green","Brown"];
 var hairColour = String["Blonde","Brown","Red","Black"];
 var dressColour = String["Grey","Brown","Blue","Black"];
 
 GUI.Label ( Rect( (Screen.width/2)-575, Screen.height -250, 250, 50), "Eyes: ");
 if (GUI.Button( Rect( (Screen.width/2)-475, Screen.height - 250, 250, 50), eyesColour[0]))
     {}
 GUI.Label ( Rect( (Screen.width/2)-575, Screen.height -200, 250, 50), "Clothing: ");
 if (GUI.Button( Rect( (Screen.width/2)-475, Screen.height - 200, 250, 50), dressColour[0]))
     {}
 GUI.Label ( Rect( (Screen.width/2)-575, Screen.height -150, 250, 50), "Hair: ");
 if (GUI.Button( Rect( (Screen.width/2)-475, Screen.height - 150, 250, 50), hairColour[0]))
     {hairColour++;}
Comment
Add comment · Show 1
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 Xipheas · Jan 15, 2012 at 05:59 PM 0
Share

I'm sorry to Necro this, but it seemed to make sense to add to this topic, and keep all the information in one place.

I have the same problem as the OP, that is that I want to define the text if a GUI.Button using a string array, but my string is defined in a different script.

For example:

//to pick up variables defined in other script called tacticalvariables.js

var tacticalvariable : tacticalvariables; tacticalvariable = gameObject.GetComponent(tacticalvariables);

OnGUI () { for (var i:int = 0; i<=5; i++) { GUI.Button (Rect (270, 10 + (60 * i), 120, 40), tacticalvariable.phaser.Fire$$anonymous$$ode[i]); } }

where tacticalvariable.phaser.Fire$$anonymous$$ode[i] is a String variable that is defined in tacticalvariables.js

I can't get anything to show on the button though - why not?

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by aldonaletto · Dec 27, 2011 at 02:31 AM

The string arrays are badly declared, what's producing this error. But there are other errors too: you must place the GUI code inside the function OnGUI, and you should have index variables eyes, hair and dress to indicate which are the currently selected options - you can't increment a string array (like in hairColour++), only its index.
The fixed code is:

var eyesColour: String[] = ["Blue","Green","Brown"]; var hairColour: String[] = ["Blonde","Brown","Red","Black"]; var dressColour: String[] = ["Grey","Brown","Blue","Black"]; var eyes = 0; // currently eyes color selected var hair = 0; // currently hair color selected var dress = 0; // currently dress color selected

function OnGUI(){ GUI.Label ( Rect( (Screen.width/2)-575, Screen.height -250, 250, 50), "Eyes: "); if (GUI.Button( Rect( (Screen.width/2)-475, Screen.height - 250, 250, 50), eyesColour[eyes])){ eyes = (eyes+1) % eyesColour.length; // rotate eyes inside the options } GUI.Label ( Rect( (Screen.width/2)-575, Screen.height -200, 250, 50), "Clothing: "); if (GUI.Button( Rect( (Screen.width/2)-475, Screen.height - 200, 250, 50), dressColour[dress])){ dress = (dress+1) % dressColour.length; } GUI.Label ( Rect( (Screen.width/2)-575, Screen.height -150, 250, 50), "Hair: "); if (GUI.Button( Rect( (Screen.width/2)-475, Screen.height - 150, 250, 50), hairColour[hair])){ hair = (hair+1) % hairColour.length; } } Read the variables eyes, hair and dress to know the currently selected color for each item.

Comment
Add comment · Show 3 · 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 blutwurstchen · Dec 27, 2011 at 04:12 AM 0
Share

Thanks for the help.

The GUI still does not use the strings from the script, they need to be added in the inspector, but then it works fine.

avatar image Eric5h5 · Dec 27, 2011 at 07:44 AM 1
Share

It uses strings from the script for the initial setup. You can also use Reset to change back to the default script values.

avatar image aldonaletto · Dec 27, 2011 at 11:54 AM 0
Share

It's like @Eric5h5 said: the Editor has an elephant memory, and remember the old values to death - even if you change them in the script. Click the small "gear" button at the right of the script name and select option "Reset" to update the Inspector variables.

avatar image
1

Answer by flamy · Dec 27, 2011 at 07:14 AM

 GUI.Label ( Rect( (Screen.width/2)-575, Screen.height -250, 250, 50), "Eyes: ");
 if (GUI.Button( Rect( (Screen.width/2)-475, Screen.height - 250, 250, 50), eyesColour[0]))
     {}
 GUI.Label ( Rect( (Screen.width/2)-575, Screen.height -200, 250, 50), "Clothing: ");
 if (GUI.Button( Rect( (Screen.width/2)-475, Screen.height - 200, 250, 50), dressColour[0]))
     {}
 GUI.Label ( Rect( (Screen.width/2)-575, Screen.height -150, 250, 50), "Hair: ");
 if (GUI.Button( Rect( (Screen.width/2)-475, Screen.height - 150, 250, 50), hairColour[0]))
     {hairColour++;}


in the above code you are changing drawing the same text again again, not the arry of strings because the in the text parameter of the button u r using eyesColor[0], hairColor[0] and dressColor[0]

instead of that you have to declare an index controller for each array like this jus rewrite as below,

 var eyeIndex=0;
         var dressIndex=0;
         var hairIndex=0;
             GUI.Label ( Rect( (Screen.width/2)-575, Screen.height -250, 250, 50), "Eyes: ");
             if (GUI.Button( Rect( (Screen.width/2)-475, Screen.height - 250, 250, 50), eyesColour[eyeIndex]))
                 { 
     eyeIndex++;
     }
             GUI.Label ( Rect( (Screen.width/2)-575, Screen.height -200, 250, 50), "Clothing: ");
             if (GUI.Button( Rect( (Screen.width/2)-475, Screen.height - 200, 250, 50), dressColour[dressIndex]))
                 {  
     dressIndex++;
     }
             GUI.Label ( Rect( (Screen.width/2)-575, Screen.height -150, 250, 50), "Hair: ");
             if (GUI.Button( Rect( (Screen.width/2)-475, Screen.height - 150, 250, 50), hairColour[hairIndex]))
                 {hairColour++;
     hairIndex++;
     }

hope it helps :)

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 blutwurstchen · Dec 27, 2011 at 03:55 PM 0
Share

I guess I never quite understood that the Index keeps the place in the array. Thanks for the tips

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Create GUI based on an array 2 Answers

Gathering GameObjects and then creating a button for each GameObject?? 2 Answers

How to output a file to a single string? 1 Answer

how to change a gui button texture 1 Answer

sort gui button 0 Answers


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