- Home /
Is it possible to show Static Variables in the Inspector?
I am cleaning up the script in my game by moving all of the variables into one organized script. To make them available to all scripts I set the as "static var". It's been working/looking great so far.
Now I have a little problem. I use
var newObject : Transform;
for instantiating objects. I want to put all of these instantiatable object variables in my Variables script. However, when I set them as
static var newObject : Transform;
they vanish from the inspector. I need them to be in the inspector to set certain prefabs to these transform variables.
Is there some way I can make these viewable in the inspector? Or could I set prefabs within the script itself?
Thanks!
Okay so he asked if it was possible and the question wasn't answered...is it possible?
Yeah, I figured it out after I hit submit, but your comment isn't obvious at first as to why unless you are thoroughly familiar with the technique. After doing some reading and testing it made more sense. So for those who may stop by here later...here's the long version of Eric's comment...correct me if I am wrong.
The problem with static functions is you can't access non-static member properties of the class that contains the static functions. Why? Because although the functions are static and exist the member properties may not exist in memory until the class is instantiated. And if you try to make the class properties static so that you can access them anytime then you'll find that you can't see them in the Unity inspector. [head scratch time] The solution (as Eric5h5 recommended) is to create a singleton which have an "instance" property that points to itself and is instantiated on startup. With that solution you can now create public "non-static" member properties that will show up in the inspector (provided you inherit from $$anonymous$$onoBehavior) and you can now get at those member vars from within the static functions using the "instance" property.
Answer by Eric5h5 · Aug 06, 2010 at 03:29 AM
Use a singleton instead of making the variables static.
isn't the singleton ultimately still a static var? http://www.unifycommunity.com/wiki/index.php?title=Singleton
How would you use a singleton? public bool smgT1Equipped;
Answer by SasugaShogun · Jan 19, 2014 at 06:09 AM
It IS possible to see static variables in the inspector:
In the upper right hand corner of the inspector window there's a little lock button, next to that is an icon of a tiny arrow pointing down next to three lines.
Select the icon of the arrow & three lines, you'll get a drop-down menu. Select "Debug" instead of Normal and you'll be able to see static variables.
It can be very useful for seeing what's going on inside your scripts.
Thank you for that little tip. I still can't see static variables with it on, but I'm super happy to see this debug mode. It makes it much easier to see where my issues, I suppose the title is self explanatory however they're so tiny I never saw them. ;p
It doesn't show static variables but still is helpful in other ways.
This shows private variables not static variables.
Answer by drudiverse · Mar 23, 2014 at 07:32 AM
Yes, use 2 variables, a one called staticvar and one called setstaticvar, and in function awake, make static var equals the figure you set in inspector. if necessary place the code in plugins or editor folder if you need it to compile early.
#pragma strict
var setmaxsteps = 50;
static public var maxsteps = 50;
var sete = 20;
static public var e = 20;
var setinverfaces = true;
static public var inverfaces = true;
function Awake(){
maxsteps = setmaxsteps;
inverfaces = setinverfaces;
inverfaces = setinverfaces;
}
i thought that singletons were way unintuitive to learn, the descriptions i found here and on net seemed to refer to making a new class and functions rather than static vars.
as @AmirSavand said, it's not best practice.
When you create a static variable, you make sure there is only one memory space for that variable. So if you create a ton of gameobjects with that script, you will always have only one shared memory space for that variable, and the variable will get shared across all of those game objects.
But if you create a local variable, only to have it rendered on the inspector, you are creating one memory space for each instantiated game object. This might seem like not a big deal, but if you are going to have a ton of instances, then this will be a concern, because you will also have a ton of memory spaces.
Answer by geomorillo · Feb 16, 2013 at 06:37 PM
or you can use another variable to show its contents and if you modify it then the static variable also changes
public int myinspectorvar =0; // you can initialize it with wathever value you want
public int mystaticVar;
public void Update(){
mystaticVar = myinspectorvar;
}
the only problem is when the static variable is changed from another script
public int myinspectorvar =0; // you can initialize it with wathever value you want
public static int mystaticVar {get; private set;}
public void Update(){
mystaticVar = myinspectorvar;
}
Answer by Shinugami · Mar 22, 2014 at 07:38 AM
As GeoMorillo said:
or you can use another variable to show its contents and if you modify it then the static variable also changes
and then he said:
the only problem is when the static variable is changed from another script
To overcome that problem you just make a function like this:
function AdjustStatic( amount : int ){
myInspectorVar += amount;
// add both negative and positive such as 5 or -5
// Use gameObject.transform.SendMessage("AdjustStatic", 5) to adjust the...
// variable from another object. Only adjust the static value from inside the object.
}
This is assuming you are updating the value :
function Update(){
mystaticVar = myInspectorVar ;
}