- Home /
How to change button text in simple character shop ?
Hi, I have a question. How to change Button's text when the player is equipped? I want when I select a character the button of the selected character to say "equipped", and the button of the other to say "select" and vice versa. I use 2 scripts for the shop.
This is the first script for the shop:
using System.Collections; using System.Collections.Generic; using UnityEngine; using System.IO; using UnityEngine.UI;
public class SelectPlayer : MonoBehaviour {
int currentPlayer = 0;
public GameObject[] Select_Player;
public int[] buyPlayer ;
int Coins;
public int[] Cost_Player;
string[] value_player = new string[4];
// Use this for initialization
void Start () {
Coins = CoinsManager.Coins;
for (int i = 0; i < Select_Player.Length; i++) {
value_player [i] = "" + i;
Select_Player [i].SetActive (false);
buyPlayer [i] = PlayerPrefs.GetInt (value_player [i]);
}
}
// Update is called once per frame
void Update () {
for (int i = 0; i < Select_Player.Length; i++) {
buyPlayer [i] = PlayerPrefs.GetInt (value_player [i]);
if (i == buyPlayer [i]) {
Select_Player [i].SetActive (true);
}
}
}
public void BuyCharact(int id){
if (Cost_Player[id] <= Coins) {
CoinsManager.AddCoins (-Cost_Player[id]);
PlayerPrefs.SetInt (value_player [id],id);
}
}
public void Reset(){
for (int i = 0; i < Select_Player.Length; i++) {
PlayerPrefs.SetInt (value_player [i],0);
}
}
public void Select(int id){
PlayerPrefs.SetInt ("currentPlayer", id);
}
}
and this is the second for the game:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class GetPlayer : MonoBehaviour {
int currentPlayer;
public GameObject[] Player;
// Use this for initialization
void Start()
{
currentPlayer = PlayerPrefs.GetInt("currentPlayer", currentPlayer);
Debug.Log("" + currentPlayer);
for (int i = 0; i < Player.Length; i++)
{
if (currentPlayer == i)
{
Player[i].SetActive(true);
}
}
}
// Update is called once per frame
void Update()
{
}
public void SelectCharacter()
{
Application.LoadLevel("Shop");
}
}
Answer by KoenigX3 · May 31, 2020 at 07:45 PM
You need references to the Button or the Text component or those buttons. Then you can simply access and modify the text:
using UnityEngine.UI
public class ButtonChanger : MonoBehaviour
{
const string selectText = "Select";
const string equippedText = "Equipped";
public Text[] buttonTexts;
int currentPlayer = 0;
public void ChangeCharacter(int characterIndex)
{
buttonTexts[currentPlayer] = selectText;
currentPlayer = characterIndex;
buttonTexts[currentPlayer] = equippedText;
}
}
In this example i used Text references. You need to assign those in the Inspector. You also need to assign the ChangeCharacter function to the Buttons, you can do that in the Inspector under OnClick(). Make sure to change the indices accordingly (Button 1 should call ChangeCharacter(0), Button 2 should call ChangeCharacter(1), and so on).
Another thing to mention is that you should not use Update like this. SetActive() is not something you would use in Update. Call it only once, and when you need to change things, call another function for it, but don't do it in every frame.
I try this, but it gives me an error "You can't convert string to UnityEngine.UI.Text ". What to do?
Your answer
Follow this Question
Related Questions
How to stop a video using the OCULUS script MoviePlayerSample.CS. 0 Answers
How can i make both two cameras to follow the player but only one with control on player ? 0 Answers
Tron type Game Question 1 Answer
Character gets stuck where he spawns??? 1 Answer
How can i make enum choice to not pause the game ? 0 Answers