- Home /
Coin Collecting: Updating UI Text
I may not be the only one having trouble with this but here goes:
I'm working on currency for my game starting with getting a working UI text element for the coins the player can collect. I need each coin laying on the ground to give a random number between 100 and 500 and add it to the current amount. I have my coin game object in my scene, the UI Text on the corner of my screen, all I need is to link the two. My errors are telling me int cannot convert to GUIText nor can I convert a type float to string. Hoping I can get some help with this, here's my code:
using UnityEngine; using System.Collections; using UnityEngine.UI;
public class CoinCollect : MonoBehaviour {
public float minCoin = 100;
public float maxCoin = 500;
float coinAmount;
public GUIText totalCoins;
public int currentCurrency = 0;
void Start() {
coinAmount = Random.Range (minCoin, maxCoin);
totalCoins = currentCurrency;
}
void Update () {
transform.Rotate (new Vector3(0,Time.deltaTime*50,0));
}
void OnTriggerEnter(Collider other) {
if (other.gameObject.tag == "Player") {
AudioSource source = GetComponent<AudioSource> ();
source.Play ();
UpdateScore ();
}
}
void UpdateScore () {
totalCoins.text = currentCurrency + coinAmount;
}
}
Answer by jmgek · Feb 07, 2017 at 09:19 PM
First you need to cast it as a string, then I would change the way you're handling these, seeing how you're mixing up things that should be static and not using proper patterns even though it does look like you're trying to. You want your coins to handle their own logic, you don't want your Coin Collect to handle things like rotation, so:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Coin : MonoBehaviour
{
public int coinAmount;
void Update() {
transform.Rotate(new Vector3(0, Time.deltaTime * 50, 0));
}
void OnTriggerEnter(Collider other) {
//Don't use tags, they're slower
if (other.gameObject.name == "Player") {
AudioSource source = GetComponent<AudioSource>();
source.Play();
UpdateScore();
}
}
static void UpdateScore() {
//You want this to be static because it's a function shared among all the Coins so only one instance of it.
CoinCollect.totalCoins.text = (string)(CoinCollect.currentCurrency += this.coinAmount);
}
};
public class CoinCollect : MonoBehaviour {
public int minCoin = 100, maxCoin = 500;
public static GUIText totalCoins;
public static int currentCurrency = 0;
void Start() {
Coin coin = new GameObject("coin").AddComponent<Coin>();
//Put your Coin gameobject where ever you need it
coin.coinAmount = Random.Range(minCoin, maxCoin);
this.totalCoins.text = (string)currentCurrency;
}
Thanks for the reply! So @jmgek if tags are slower, what would you recommend?
Either by 'gameobject.name' Like I wrote. Or since you will only have one player use your player perfab as a comparison.
public static GameObject myPlayer;
if (other.gameObject== myPlayer) {
Erm, I have three selectable characters for my game at the start XD that's why I went with tags to begin with
You were right @jmgek, that function needed to be static :)
It's Easier to just use "" and add the currency to it. But this also works!
Answer by BlockFade · Feb 08, 2017 at 09:24 AM
This should work in the UpdateScore function.
void UpdateScore () {
totalCoins.text = "" + currentCurrency + coinAmount;
}
I tried something like this for that function and it seems to work:
void UpdateScore () { Debug.Log ("Score Updated"); totalCurrency += currentCurrency; totalCoinsDisplay.text = "" + totalCurrency; }
This is a way to do it but it's not the best practice, seeing how you're concatenating to a empty string, you could run into problems, it's always better to cast to a type:
string myString = (string)45;
Your answer
Follow this Question
Related Questions
Graphic problem with UI Text 2 Answers
New UI: change text 2 Answers
UGUI Find tapped character in textfield? 1 Answer