Found another solution
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;
}
}
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;
}
}
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
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
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