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 Madix · Dec 05, 2014 at 11:50 AM · buttontext4.6

How to replace Button Text when clicking another button?

Hello,

I'm using Unity 4.6 and new to both Unity and c#.

How would I go about replacing the text within a button with the text from another button?

Say I have 5 buttons, the first button would be the main one that gets updated. I would like the text within that button to be replaced with the text from whichever other button the user clicks. So if I were to click Button 4 that says Fish, Button 1 would then also say Fish. Then if i were to click a different button it would update again to the text of the last button clicked. Example below.

  • Button 1: TempText

  • Button 2: Dog

  • Button 3: Bird

  • Button 4: Fish

  • Button 5: Bear

I've watched the tutorials and read other Unity answers on how to do this with the score, but as I'm new to this I'm not sure how to adapt it to work with clicking on a button to update another button. Would someone be kind enough to link me to a tutorial that explains how to do this or maybe provide example c# code?

Thank you for your time and assistance. I apologize ahead of time if there is a resource explaining exactly this, I haven't had luck finding it.

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
1
Best Answer

Answer by jenci1990 · Dec 05, 2014 at 12:50 PM

  1. Select your "Dog" button and add OnClick event: alt text

  2. Drop your TextObject to it:

alt text

....


tut.jpg (50.4 kB)
tut2.jpg (51.3 kB)
Comment
Add comment · Show 4 · 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 jenci1990 · Dec 05, 2014 at 12:53 PM 0
Share
  1. Select from the functions the "Text/string text" component.

alt text

4.and set the new name: alt text

You can do it in the other buttons.

tut3.jpg (61.1 kB)
tut4.jpg (49.7 kB)
avatar image Madix · Dec 05, 2014 at 05:12 PM 0
Share

Thank you very much! I was trying to figure out how to do it with the On Click events at first but couldn't get it. I've marked this as Answered as it does what I want, I appreciate that. Curious, is there a way to auto populate the text rather than having to type the string for each one? Just trying to think of how to save time if this was done on a much larger scale. For now I don't $$anonymous$$d doing it as it fulfills my needs. Again, thank you for your time and assistance on this, much appreciated.

avatar image jenci1990 · Dec 05, 2014 at 06:41 PM 0
Share

You can do it by script. This scrip change the target Text to the button name automatically. Add this script to all button (except the contolled button), and assign the "targetText" variable.

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class SetButtonText : $$anonymous$$onoBehaviour {
     public Text targetText;
     private Button myButton;
 
     void OnEnable() {
         myButton = GetComponent<Button>();
         myButton.onClick.AddListener(() => {ChangeTarget();});
     }
 
     void ChangeTarget() {
         string myString = GetComponentInChildren<Text>().text;
         targetText.text = myString;
     }
 }

avatar image Madix · Dec 05, 2014 at 08:47 PM 0
Share

Very cool. Thank you so much for going out of your way to show me how to do this. =)

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Canvas Button persistence 0 Answers

v4.6 Create GUI Elements Via Script? 1 Answer

How do I access the button component of a button in the new gui system. 2 Answers

Text isn't showing up on button 0 Answers

4.6 UI: How to invoke button click on Enter key 2 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