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 /
avatar image
0
Question by McoGames · May 31, 2020 at 06:54 PM · scripting problemscript.charactershopin-game

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");
 }

}

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

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.

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 McoGames · Jun 05, 2020 at 08:48 PM 0
Share

I try this, but it gives me an error "You can't convert string to UnityEngine.UI.Text ". What to do?

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

280 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 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 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 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 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 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


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