Question by
seyong040106 · Aug 07, 2016 at 07:18 AM ·
c#textvariable
How to print variable? (C#)
I want to print a variable "coin" by text(UI). How to make it?
Comment
Best Answer
Answer by Yujingping · Aug 08, 2016 at 06:57 AM
First you definitely need a Canvas and a Text gameObject. So lets call the Text "VariableText". Secondly you need to set up a reference to the VariableText to alter its content.
public Text variableText;
Then you can change variableText.text to the text that you wanna to display. Something like this :
variableText.text = "My Coins: " + coins;
Just for integrity let me post a full C# script:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class CoinShower : MonoBehaviour
{
public Text variableText;
public int coins;
public void ShowCoins ()
{
variableText.text = "My Coins: " + coins;
}
}