UI Button Needs to change text depending on variable

I need to have my UI buttons for attacks in a turned based system read different text depending on what attacks the available character knows / I need the UI button text to reference a variable. For example if character one is being played I need the attack 1 button to be punch but character 2’s attack 1 is kick. BTW I used the gameobject UI system, not the code based UI system. 45838-fightmenu2.png

Here are some suggestions, which you can search on the web: From your manager script, find a reference to your UI button text component (in the simplest case a public variable of type UnityEngine.UI.Text which you fill via the Inspector). Then access yourTextComponent.text to set it to a new string. To change the action of the button, you will need to deal with the onClick event of the Button component, but that’s a bigger story. Start with this tutorial for updating UI components: link text When that is working you can search for Unity UI Button onClick event to see how you can assign different event based actions or delegate methods to the button.

You could create a script, that checks for certain things in a specific way, depending on how you want to do it, you could use a bool/boolean, a int, a string, whatever youd like, I normally use ints when I have more than 3 characters, otherwise I use strings. Something like:

C#:
//You'll have to import at the top of the script: UnityEngine.UI;

Public int characterNumber = 0;
Public Text buttonOne;

void Start () {
if(characterNumber == 0){
buttonOne.text = "Punch";
}
else if(characterNumber == 1){
buttonOne.text = "Kick";
}
//You could change int to string, and have it by another method like such:
//if(characterName == "Bob"){
//buttonOne.text = "Punch";
//}
//else if(characterName == "Joe"){
//buttonOne.text = "Kick";
//}
}

JAVASCRIPT:

characterNumber : int = 0;
buttonOne : UI.Text;

function Start () {
if(characterNumber == 0){
buttonOne.text = "Punch";
}
else if(characterNumber == 1){
buttonOne.text = "Kick";
}
//You could change int to string, and have it by another method like such:
//if(characterName == "Bob"){
//buttonOne.text = "Punch";
//}
//else if(characterName == "Joe"){
//buttonOne.text = "Kick";
//}
}

You would put this script on a gameobject that is never going to change, such as your main camera (unless you delete it at some point in game at runtime).

If you want, you could set up a “class”/“character” system with presets, but youd have to do some script referencing for that, I always personally hate doing that… Basically, you would have one script setup when the player is choosing their character, and once tey choose it, a few variables will be set up, being something like:
character = “Joe”; //(character being a string ofcourse)
then in that same script, a setup for:

fightMove1 = “Punch”;
fightMove2 = “Jab”;
fightMove3 = “Head-butt”;
fightMove4 = “elbow”;

IF character == joe for example, and then those variables would change IF character == bob.

Then another script setup in game/in your arena/map that checks that script basically, to load the fight moves into the buttons you have setup. In that example, fightMove(x) would be a string.

It really depends all on HOW you want to set it up, it could be done in a few ways, those are 2 common ways id do it. Hope that helped.