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 Fierce Waffle · Jan 31, 2011 at 05:31 AM · guibuttontextchange

Change Text of GUI Button from Script

I am trying to make it so when the button is pressed it changes its own text. Heres what Ive been trying with no success.

var achButton = GUILayout.Button("Show Achievement List");

 if (achButton)
  {
     achButton.text = "Hide Achievement List";
 }

help is appreciated.

Comment
Add comment · Show 2
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 yoyo · Jan 31, 2011 at 05:52 AM 0
Share

The BUtton method is called every frame, you can pass in any text you like. When the button is pressed the method returns true -- when this happens you should set a variable so you know to change the text.

avatar image Fierce Waffle · Jan 31, 2011 at 06:06 AM 0
Share

im aware if that but I dont know how to input text.

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Jean-Fabre · Jan 31, 2011 at 06:14 AM

Hi,

This is one way to do it:

Basically, you create a boolean variable AchievementVisible and this is what you toggle when the user actually click on the button, then you run a function that update both the text of the button and also show or hide the achievement gui. Notice that I store the button's label in a separate variable, my OnGUI function becomes clearer and not cluttered.

public var AchievementVisible:boolean = true; public var HideText:String = "Hide Achievement List"; public var ShowText:String = "Show Achievement List";

private var currentText:String;

function Start(){

 UpdateAchievementVisibility();

}

function UpdateAchievementVisibility(){

 if (AchievementVisible){
     currentText = HideText;
 }else{
     currentText = ShowText;
 }

}

function OnGUI(){

 if (GUILayout.Button(currentText)){
     AchievementVisible = ! AchievementVisible;
     UpdateAchievementVisibility();
 }

 if (AchievementVisible){
     GUI.BeginGroup (Rect (Screen.width / 2 - 50, Screen.height / 2 - 50, 100, 100));    
         GUI.Box (Rect (0,0,100,100), "Achievment");
     GUI.EndGroup ();
 }

}

Hopefully the rest is all self explanatory.

Bye,

Jean

Comment
Add comment · 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
2

Answer by T. · Jan 31, 2011 at 06:56 AM

Create a string and store what you want displayed in that, then update the contents of the string and the buttons text will update along with it.

Heres some psudeo code

var buttonText : String = "Show Trophies"; var buttonPressed : Boolean = false;

if Button if(!buttonPressed) buttonText = "Hide Trophies" else { buttonText = "Show Trophies" buttonPressed = True; }

Hope that helps! :)

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 Bunny83 · Jan 31, 2011 at 11:57 AM 0
Share

You've messed up your script... plz fix it.

avatar image T. · Feb 01, 2011 at 02:41 AM 0
Share

it wasn't intended to be copy/pasted. only the logic for doing it.

avatar image Jerz T. · Nov 16, 2016 at 01:27 AM 0
Share

it's much more helpful to have the code so we can understand how it works. the script objects, the format, etc. it's all needed for the logic to work.

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

1 Person is following this question.

avatar image

Related Questions

Change text gui.button in a for bucle 1 Answer

GUI & GUI Text Disappear When Publishing 6 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

How can I combine GUI button with script that is put on different gam objects? 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