I have a player script which is using the get/set methods to forward money status to a GUI script. And in the GUIHandler script it get’s myPlayer.Money and puts it in a label, although when I update in my Player script it doesn’t update in the GUI script. I’ve been using the Properties script tutorial thingy from Unity’s official learning website, intermediate scripting course.
Here is the player script (player.cs)
using UnityEngine;
using System.Collections;
public class player : MonoBehaviour {
public int money;
public int Money
{
get
{
//Some other code
return money;
}
set
{
//Some other code
money = value;
}
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
money++;
}
}
And here is the GUIControls script (GUIControls.cs)
using UnityEngine;
using System.Collections;
public class GUIControls : MonoBehaviour {
public GUIStyle moneySkin = null;
public Texture icon;
public Texture money;
public int playerMoney;
void Update () {
player myPlayer = new player();
playerMoney = myPlayer.Money;
Debug.Log(myPlayer.Money);
}
void OnGUI () {
//GUI.Box (new Rect (10,10,100,50), new GUIContent("This is text", icon));
//GUI.DrawTexture(Rect(10,10,60,60), icon, ScaleMode.ScaleToFit, false, 10.0f);
//myPlayer.ToString();
GUI.DrawTexture(new Rect(10,Screen.height - 160,120,120), icon); //brain
GUI.depth = 0;
GUI.Label (new Rect (30,Screen.height - 150,100,50), "Brainwashiness");
GUI.DrawTexture(new Rect(10,10,50,50), money); //money
GUI.depth = 0;
GUI.Label (new Rect (70,10,100,50), ""+playerMoney, moneySkin);
}
}
How can it increment the value you see almost at the bottom (“”+playerMoney) when it’s updating in the Player script?
Thanks!