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?

The answer was implied in my answer. Namely, no.

10 Answers

10

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

The variables aren't static, no.

@ina indeed, singleton are "ultimately a static var" at all

How would you use a singleton? public bool smgT1Equipped;

In my case, i use 1 script in different scenes, so singleton script don't have instance from other scene. I had to init array of values (15 elements with 4 fields each) in each scene. What can i do in this case?

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.

That's interesting, but it's not best practice.

This works! Very simple. Screw best practices, I'm not making an open-world game, just something simple.

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.

Did that...doesn't work

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.

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; }

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 ;
}

You can also do something akin to this; it has worked for me:

[CustomEditor(typeof(OtherObject))]
public class OtherObjectEditor : Editor {
    public override void OnInspectorGUI() {
        StaticClass.staticEnumObject= (ENUM_TYPE)EditorGUILayout.EnumPopup("text next to enum field", staticClass.staticEnumObject);
    }
}

public static class StaticClass{
    public static ENUM_TYPE staticEnumObject = ENUM_TYPE.DEFAULT_ITEM;
        
    public enum ENUM_TYPE {
        DEFAULT_ITEM,
        ...
    }
}

public class OtherObject: MonoBehaviour{
...
// if you want it to reset on application stop
void OnApplicationQuit() {
        StaticClass.staticEnumObject = StaticClass.ENUM_TYPE.DEFAULT_ITEM; 
}

// Alternatively you can make an inspector button to do this for you outside of the application running.
}

here, take this easy solution:

public static int number;
public int _number;

private void Awake()
{
    number = _number;
}

you can change the static variable now in the inspector by having another variable which refers to the static one.
in my case its in the awake method, because i want to set it before the start method, so other scripts can get the correct number in the their own start method

cheers

This question was already asked and answered:

How does one inspect static vars?

For your specific case, however, I'd agree with Eric: Using a Singleton is the preferred method over using static vars to achieve what you're trying to achieve.

the link leads back to the same question?

http://answers.unity3d.com/questions/11098/how-does-one-inspect-static-vars.html

https://forum.unity3d.com/threads/how-to-show-a-static-list-in-unity-inspector.469253/

This worked for me:

public class Hello{
    public static int sOne;
    public int One;  //this variable will be shown in inspector
     
    void Update()
    {
    sOne = One;
    }
}

public class Hi{
// get static variable from Hello class
 private int One = Hello.sOne;

}