- Home /
How does one inspect static vars?
Debug mode in the inspector only reveals private vars. I understand that static vars are global, but there has to be an easy way to see, say, a static array, that doesn't involve scripting a gui readout.
Answer by CJCurrie · Feb 04, 2010 at 11:40 PM
While not the most effective solution, I did find a workaround. If you know the class of your object you can create a public var of that class (class MUST be defined) and set it equal to your static var each frame. The limitation of this approach is that data types such as Arrays (things that aren't serializable because they may contain items of different classes) can't be viewed in the inspector. : (
Answer by jashan · Jun 15, 2010 at 10:06 AM
You can even take it one step further: You can keep track of the last value in your local property that is shown in the inspector. Then, in each update, check if that local value has changed, and if it has, set the static variable. In other words: You keep two local variables, the one exposed in the inspector and another one that could even be private. In Awake(), you'd set both the private and static variable (pull the value from the public variable). In each Update(), if the private variable is different from the public one, set both the static variable and the private one to the value of the public one (that obviously just has changed).
That way, you can not only inspect the static variable - you can also change it from the inspector.
If you're using a lot of static variables, you might consider creating one "static variables manager object" that controls all of them ("one ring, um, class to rule them all" ;-) ).
I've done this as well - but I would check for changes to instance variables inside of OnValidate() and then change the static variables if needed. Then, only changes to the instance fields inside the inspector affect the static fields. I also used this to be able to change values in an outside config file from within the inspector.
The problem with doing this in Awake/Update becomes apparent if you have multiple objects created at different times. If object one is created, and you set the static variable in awake, then set it to a different value later in the game, you probably don't want the static value being overwritten just because a new object was created. Or maybe you do. Just something to consider.
Answer by yoyo · Feb 03, 2012 at 06:09 AM
/shameless plug/ I've used .NET reflection to create a new editor window that can inspect any field or property of any Unity or .NET object, including public, private and static variables. There's a screenshot of an earlier version of the tool here, and a list of current and planned features on this Trello board. I will release this tool in the Asset Store in the not too distant future. I'm looking for a few more beta testers if you're interested -- you'll get a free copy and my eternal gratitude! Drop me a note if you're interested in beta testing, or just want to talk about .NET reflection techniques. You can reach me at yoyo (at) zeroandone (dot) ca.