I have the following script attached to a unity UI text object. it displays player 1’s money.
I feel however that this is not proper coding practice since if I have multiple players, I would need to create a separate script for each one and attach that each script to a separate text field. Is there a better way of doing this?
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class TextScript : MonoBehaviour {
public Player player1;
private Text text;
private int money=0;
private string s_money;
private GameObject obj;
void Awake() {
text = GetComponent <Text> ();
text.text = "test";
GameObject goPlayer = GameObject.Find("PlayerGameObjectName");
if (player1 != null) {
player1 = goPlayer.GetComponent<Player>();
}
}
void Update(){
text = GetComponent <Text> ();
money=player1.Money;
s_money=money.ToString();
text.text = "you have " + s_money+ " Dollars";
}
}