This should be easy, but I cannot work out how to do it, nor can I find any examples or posts regarding it.
I have three scripts, each handling a dynamic value - ScriptA has a ‘health’ variable (int), ScriptB has a ‘rotation’ value (float), and ScriptC has a ‘position’ variable (Vector3).
I’m trying to write a GUI script that will continuously write the current values of each of these three variables to a HUD.
How do I do that?
I’ve been using GetComponent in the Start function of my Gui script, but then the values don’t update dynamically.
Any suggestions most appreciated.
EN
Use three variables. Have each one store a different script.
var scriptA : ScriptAName;
var scriptB : ScriptBName;
var scriptC : ScriptCName;
Connect those scripts to the objects using GetComponent, then link your GUI to the scripts’ respective variables directly.
scriptA.variableName
Thanks Gargerath, but there is a problem. If I write:
var scriptA : ScriptAName;
the compiler gives this error: The name ‘ScriptAName’ does not denote a valid type (‘not found’).
I fixed it by using AddComponent, then accessing the variable. Thanks for your help, and sorry for the bother.
EN
Uhh… I didn’t mean for you to use the code literally. Replace the “ScriptAName” with the name of your script. Capitalization matters.
LOL, I realise that, still GetComponent throws an error where AddComponent doesn’t. Thanks.
Try putting the name of the script in quotes inside GetComponent. AddComponent is creating a new script instead of using the one you have there already, after all.
Thanks Gargerath, I’m using C# and quotes don’t work at all. I’ve looked back over an earlier post about GetComponent vs AddComponent, and realise I am going about it the wrong way anyway. Thanks for your advice so far.
EN