- Home /
Dynamically pass integer values into separate scripts in C#?
Hey all, I have a very simple task but can't seem to find the Answer any where. I need to pass the total amount of money remaining from the shopping GUI display into the Money HUD (guiLabel) display. The Shop display is on a different script then the HUD display. .It is this way because the shopping display only displays when you enter a shop, and the Money HUD display is always visible, so that you can always know how much money you have. I need for the shop total to dynamically update the Money HUD total when ever i go shopping. The shopping total is a labeld display and the Money Hud is also a labeled display.
enter code here
public int Shoppingtotal;
GUI.Label(new Rect(150,80,700,20),""+Shopping total); // i need this total to dynamically update into
;
;
public int MoneyHUD;
GUI.Label(new Rect(150,80,700,20),""+MoneyHUD); // into this total which is found in another script.
thank you very much!
Answer by khalil · Jul 25, 2011 at 10:11 PM
The solution to this problem was found through GameObject .SendMessage.
using UnityEngine; using System.Collections;
public class BaseButtons :BaseShop { void neededYethiore(float yethiore) { print(yethiore); } void OnGUI() { GUI.Label(new Rect(100,100,100,30),""+Yethiore); if(GUI.Button(new Rect (140,100,20,20),"+")) { neededYethiore(); //adds 5 to Yethiore. gameObject.SendMessage("neededYethiore",5.0f); }
}
}
and on the receiving Display Which is attached to the scripts game object.
void On GUI()
{
GUI.Label (new Rect (30,30,30,30)""+Yethiore);
}
Answer by Graham-Dunnett · Jul 24, 2011 at 07:34 AM
I think the documentation gives you your answer (see bottom section about global variables.)
Answer by Waz · Jul 24, 2011 at 11:33 AM
So one script needs to know about the other. The simplest way (and also the cleanest way from an architectural point of view) is to have an explicit reference which you can set, so in Shopping.cs:
public Money money;
Drag the GameObject with the Money script on it to this variable in the inspector. Then Shoppibg can talk to Money:
money.MoneyHUD -= ShoppingTotal;
Or whatever interaction it is that you need to have between the scripts,
Answer by khalil · Jul 24, 2011 at 01:05 PM
Thanks form both of you guys.
But your descriptions of the problem as well as the reference material, are all simply to vague and without the amount of detail that one would need to actually implement your remedies. Could you both or at least one of you be way more descriptive and a bit less short ended? Thanks again. ~K