Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
This question was closed Dec 31, 2015 at 10:00 PM by KnightRiderGuy for the following reason:

Found another solution

avatar image
0
Question by KnightRiderGuy · Dec 31, 2015 at 02:25 PM · c#uitexttoggletoggle button

Toggle UI Text With UI Toggle Button

I have a UI Text object on a canvas that I'm calling with this method but I'm having a difficult time trying to figure out how to get my UI toggle button to switch between the two text messages in Button State 0 & 1

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class MessageCentreManager3 : MonoBehaviour {
 
     int sysHour = System.DateTime.Now.Hour; //gives you the current hour as an integer.
 
     //UI Text Reference
     public Text MessageCentre3Text;
     public Toggle someToggle;
 
     //Button States
     public int buttonState = 0;
 
 
 
     void Update ()
     {     
         if (buttonState == 0) MessageCentre3Text.text = "VOICE PROJECTION SCREEN";
         else if (buttonState == 1) MessageCentre3Text.text = "VOICE PROJECTION";
         else if (buttonState == 2) MessageCentre3Text.text = "ANHARMONIC SYTHESIZER";
     }
 
 
 
     //Default Message
     public void GoDefaultMessage()
     {
         buttonState = 0;
     }
 
     //Voice Projection Activated
     public void GoVoiceProjectionMessage()
     {
         //buttonState = 0; Toggle ButtonState 0 Message
 
         //Toggle
 
         //buttonState = 1; Toggle ButtonState 1 Message
 
     }
 
     //Anharmonic Synthesizer Activated
     public void GoAnharmonicMessage()
     {
         buttonState = 2;
     }
 
 
 }
 
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 KnightRiderGuy · Dec 31, 2015 at 03:11 PM 0
Share

O$$anonymous$$ I tried this and I can get my UI text to toggle back on forth for the Toggle Button just fine but when I click on my other button and it swaps out the message and then I go back to my toggle button it no longer toggles the message.

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class $$anonymous$$essageCentre$$anonymous$$anager3 : $$anonymous$$onoBehaviour {
 
     int sysHour = System.DateTime.Now.Hour; //gives you the current hour as an integer.
 
     //UI Text Reference
     public Text $$anonymous$$essageCentre3Text;
     //public Toggle someToggle;
     public bool buttonState0$$anonymous$$essage = true;
 
     //Button States
     public int buttonState = 0;
 
 
 
     void Update ()
     {     
         if (buttonState == 0) $$anonymous$$essageCentre3Text.text = "VOICE PROJECTION SCREEN";
         else if (buttonState == 1) $$anonymous$$essageCentre3Text.text = "VOICE PROJECTION";
         else if (buttonState == 2) $$anonymous$$essageCentre3Text.text = "ANHAR$$anonymous$$ONIC SYTHESIZER";
 
     }
 
 
 
     //Default $$anonymous$$essage
     public void GoDefault$$anonymous$$essage()
     {
         buttonState = 0;
     }
 
     //Voice Projection Activated
     public void GoVoiceProjection$$anonymous$$essage()
     {
         if (buttonState == 0) {
             buttonState = 1;
         } else if (buttonState == 1) {
             buttonState = 0;
         }
 
     }
 
     //Anharmonic Synthesizer Activated
     public void GoAnharmonic$$anonymous$$essage()
     {
         buttonState = 2;
     }
 
 
 }
 

1 Reply

  • Sort: 
avatar image
0

Answer by Socapex · Dec 31, 2015 at 07:21 PM

I believe a rethink of what you want to do would simplify everything a lot. First off, there is no need to check anything in update for this type of behaviour :) I would propose something much simpler.

Use the UnityEvent of your button to call a simple method.

Make a script that you attach on the UI Text, it has 1 bool, and 1 method. Something like this:

 bool secondSentence;
 
 public void ChangeText()
 {
     if (secondSentence) {
         GetComponent<Text>().text = "My second sentence.";
         secondSentence = false;
     } else {
         GetComponent<Text>().text = "My first sentence.";
         secondSentence = true;
     }
 }

Then on the button, drag the script onto the Click event and select that method. Done :)

You can also use ternary operators to clean it up:

 bool secondSentence;
 
 public void ChangeText()
 {
     GetComponent<Text>().text = secondSentence ? "Second text" : "First text";
     secondSentence = !secondSentence;
 }

PS. Untested but should work.

Edit It seems you have more states. For that you can store your strings in an array or list, then use an int to assign the string to your text. Every time you call the method, increment the int like so:

 sentenceNum = ++sentenceNum % myArray.Length
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 KnightRiderGuy · Dec 31, 2015 at 08:17 PM 0
Share

Thanks @Socapex, I tried out your code but it did not work. I ended up finding another way was to on my other button that called in a sliding screen was to in the inspector for that button create another state and then drag the V.P button onto it and set it's bool to be off so that was when the slide in screen is called it will turn off the V.P. toggle button.

Follow this Question

Answers Answers and Comments

63 People are following this question.

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

Related Questions

How To Load TTF Font From External File 0 Answers

Display CPU usage on a UI Slider or Text 1 Answer

How do I make an external text document show only between certain lines in UI Text 1 Answer

How to properly display debug messages on UI 0 Answers

I want Toggle Group Savescript. When i restart the game. the game disable all toggle. 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