- Home /
Update in one script show in another - Properties
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!
I believe your problem is that you are using new
on a $$anonymous$$onoBehavior object (`new player()`), and therefore its Update() loop is not executed at all. You need to either Instantiate
it, or place it in the scene and have a reference to it (`public player myPlayer` then attach using the inspector).
What is the best way to instantiate it? I'm kind of new to Unity so I'm still in the learning phase if I can call it that. Haven't worked that much with instantiation so if you could give an example it would be greatly appreciated :) But thanks anyways, now when I think about it there was a warning saying something about that.. (facepalm)
Any script you have that derives from $$anonymous$$onoBehaviour needs to be attached to a GameObject in order to work. Just drag the script from your project view onto the object you want it to be attached to in the hierarchy.
Heh :) Well - seems to me like you are doing just fine, at least you have some good starting point.
First, with or without instantiate, the player myPlayer = new player();
line inside the Update loop is expensive, and I guess you did not mean it to be there. You are creating a new instance every single frame...
Now, as for avoiding that new
statement:
Option one, instantiate a Player prefab (btw, you may want to capitalize your class names)
public Player playerPrefab;
Player myPlayer;
void Start() {
player = Instantiate( playerPrefab, Vector3.zero, Quaternion.identity ) as Player;
}
For this to work, you will need to create a player prefab, and drag it to the playerPrefab variable in the inspector, let me know if need help here.
Option two, just create the player GameObject in the scene, and drag it (create a reference to it) from this script ( public Player player
)
Answer by DannyB · Sep 19, 2013 at 09:11 PM
So after we have established that the problem is with the new
statement on a MonoBehavior, here is a step by step working solution to hopefully get you in the right direction:
A. Use this Player.cs script (minimalistic, for demo purposes):
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
public int money = 0;
public int Money { get{ return money; } set{ money=value; } }
void Update () {
money++;
}
}
B. Use this for GuiControls:
using UnityEngine;
using System.Collections;
public class GuiControls : MonoBehaviour {
public Player player;
void Update () {
Debug.Log(player.Money);
}
}
C. Create an empty object in the scene, call it Player, and attach Player.cs to it.
D. Create an empty object in the scene, call it GUI and attach GuiControls.cs to it.
E. Drag the Player object onto the Player variable in the GUI object's inspector.
F. Play and watch those debug statements coming in.
Oh my god thank you! :D It worked like a charm! Gosh, this community is truly awesome :) I struggled with that problem for at least a couple of hours. Thanks again :D
Your answer
Follow this Question
Related Questions
GUI position not updated on build & run 0 Answers
Update variables with C# properties in customeditor 2 Answers
Display var on gui not working... 2 Answers
Best way to - Animation Sync - Multiple gameObjects. 0 Answers
Display a objects property. 1 Answer