GetComponent public float
Hi guys, I'm having troubles with my scripts. I'm trying to get a Float Value from Script A to the Script B so that on the Script B I can save the Float Value.
public Text coinCounter;
public Text coinPerClick;
public float coins;
I want to get the Float coins to the second script but I can't figure out how ..
Answer by Cuttlas-U · Apr 14, 2017 at 08:32 PM
hi: if your currenet script name is "Script1" and the Script that u want to get a variable from it name is "Script2" do it like this :
in the Script1 do this where ever u want to get a variable from script2 ;
float t = GameObject.FindObjectOfType().coins;
========================== this is an easy way but not optimized;
the optimize way is u first define a variable by the script u want to get value from like this :
public Script2 myScript;
then in the start method u get that script;
void Start() { myScript = GameObject.FindObjectOfType(); }
then any time and any where in your script u can easyli access that like this :
float t = myScript.coins;
u can call funcitons too :
myScript.Doit();
I can't understand, what should myScript be? Because the Script1 and Script2 are my two scripts of upgrade and clicking.. but what is myScript?