- Home /
Variables defined in editor, but clear on runtime
OK, so I have a GUI script, which gets a variable called BarkyStatus, which is a script name. The GameObject that has that script is the player, which I define. But, whenever I hit Play, the variable clears and halts the entire script.
Here is the basic formula:
• Script needs the BarkyStatus variable, which contains score data, pickup data and health data.
var scoreTexture : Texture;
var timeTexture : Texture;
var checkmarksTexture : Texture;
var gemsTexture : Texture;
var healthBase : Texture;
var healthBar : Texture;
var nativeVerticalResolution = 600.0;
var barky : BarkyStatus;
barky = GetComponent("BarkyStatus");
function OnGUI () {
var timeText = Time.timeSinceLevelLoad;
GUI.matrix = Matrix4x4.TRS (Vector3(0, 0, 0), Quaternion.identity, Vector3 (Screen.height / nativeVerticalResolution, Screen.height / nativeVerticalResolution, 1));
GUI.DrawTexture(Rect(5,5,80,20),scoreTexture,ScaleMode.ScaleAndCrop,true,0);
GUI.Label (Rect(85,6,100,23), barky.score.ToString());
GUI.Label (Rect(65,27,100,23), timeText.ToString());
}
Well, that's the incomplete version, anyway.
• Plug in the variable in the editor.
• Press Play, and the variable clears itself.
Any tips on how to prevent this? Not that this affects the core gameplay, but I kinda want to see what my score is during gameplay. Help?
Answer by Shazrock · Dec 31, 2012 at 02:38 AM
OK, here's what worked:
var player : GameObject; // Just this part in the beginning
Then, I modified the GUI.Label command...
GUI.Label (Rect(85,6,100,23), player.GetComponent(BarkyStatus).score.ToString());
This solved the problem. Thanks!
Answer by TomPendergrass · Dec 30, 2012 at 09:56 PM
I believe you're searching for BarkyStatus script only on the object this script is attached to. Try searching the owner object for the compenent.
player : GameObject; //Your player goes here!
barky = player.GetComponent(BarkyStatus);
Your answer
![](https://koobas.hobune.stream/wayback/20220613090139im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
error modifying a var 1 Answer
Script not working, help please 1 Answer
UnassignedReferenceException when its already assigned 1 Answer
Scripting error! 1 Answer
C# - variable is assigned but its value is never used 3 Answers