- Home /
How to use a static var
Hi guys. I am new to Unity and help is greatly appreciated, I have a tiny problem where I have a script with two variables each holding a game object (I am trying to make it invisible) now I am trying to hide these two gameobjects through the use of a different script, so I tried making the variables static, but once that is done the variables dissapear in the unity window and I can't drag a gameobject in anymore.
var bodyPart1 : Transform; var bodyPart2 : Transform;
These are the ones I am trying to make static, and then in a different script I am trying to access it like so
HealthControl.bodyPart1.renderer.enabled = false;
, but like I said as soon as I make them static, I can't drag a gameobject in the variables and it comes up with an error as the var becomes empty (null)
Any ideas?
Answer by dannyskim · Jul 03, 2012 at 05:25 PM
This is done on purpose. Static members of a class are global variables, and thus should not in practice be editable outside of the class, or without calling the class directly.
In normal programming, you typically don't want to use static variables because this can lead to improper accessing of said variable, which in turn could cause problems down the road. Typically, you'll want to make a variable public or private, and access it by creating a new instance of the class itself, and changing the property by accessing that instance of the class, which is one of the reasons they call it Object Oriented Programming.
In any case, like I said static variables are meant to not be accessed by the inspector. You have to set it before or during runtime in order to set it's value, with something like GameObject.Find(), Resources.Load(), etc. Do so in Start() or Awake() to initialize it, and then you can call it just fine.
Thanks for your answer, I don't really understand the Object Oriented but I guess I could predefine them within the code ins$$anonymous$$d of the inspector thanks! Any more input on what you said about making a new instance of the class and how to do that would be great as I have to be honest I don't really understand it too well.
Answer by f34rtehninja · Jul 03, 2012 at 08:42 PM
So I think you're trying to make other game objects invisible from a controller/manager type object, right?
Static variables would probably not be the way to do this, static variables are global variables that persist throughout changes in the game, it's good for things like score or player health as the player travels through several different scenes. More on global variables can be found here at the very helpful scripting reference Scripting Reference Global Variables.
What I believe you should do is instead get a reference to the object you're intending to make invisible and then access the renderer that way. You can do as follows:
var bodyPart1 : GameObject;
var bodyPart2 : GameObject;
Then save the script, attach it to your controller, drag the references over in your inspector then the following will work inside your health controller script:
bodyPart1.renderer.enabled = false;
bodyPart2.renderer.enabled = true;
The renderer component is part of the GameObject, you were trying to do it via the transform which wouldn't work.
If you wanted to access the health controller script from elsewhere you could make an accessor function in the health control script, then get a reference to that script and call the function.
var HealthControl : HealthControlScriptName;
Then drag a reference with the inspector and inside the script that accesses the HealthControlScriptName the make a function call like
HealthControl.HideBody1(true);
HealthControl.HideBody2(false);
and inside of the HealthControlScriptName you can fill out the functions like:
public function HideBody1(hideIt : Bool)
{
bodyPart1.renderer.enabled = !hideIt;
}
public function HideBody2(hideIt : Bool)
{
bodyPart2.renderer.enabled = !hideIt;
}
This, when called, will hide or unhide the body part. Do you want to hide bodyPart1? Call HideBody1(true). Unhide it with HideBody1(false). The public keyword is what you intend to do with static, I believe. It allows access through other scripts when you have the reference to the script that holds the public functions.
Make sense? This is what you needed?
Your answer
Follow this Question
Related Questions
Make Score Go Up At a Collision 2 Answers
Error Assigning GameObject To Var Inside A Class 3 Answers
Accessing a js static var from a c# script 1 Answer
Health is set to zero at start of game. 3 Answers
Static variables explanation. 2 Answers