How can i display the current state of a static var?
if u want to see it in log use Debug.Log(); or print();
if u wanna display it in the game use
GUI.Label(); ///note gui.label shd be called only from OnGUI.
Thanks flamy
So there is really no way to display a static var in the Inspector? I really need to add a GUI element for that? This is sad. A static var state is in most cases as important as all other variables. So why is there no inbuild way to display it?
You can write an Editor script for the script you wish to display the static variable for:
Edit: do note that if you want to be able to actually change the value through the inspector, it’s probably best not to allow editing the variable outside of play mode, as it is not possible to change the starting value of the static variable persistently except by changing the script defining it.
Thanks tomvds. Displaying would be more than enough
To write editor scripts is still a bit above my horizon though, unfortunately: But thanks for the hint
It should be really easy ;). Try the following (replace both occurrences of ‘MyScript’ by the name of the script with the static variable and ‘staticVariable’ by the name of the variable):
@CustomEditor(MyScript)
class MyScriptEditor extends Editor
{
function OnInspectorGUI()
{
EditorGUILayout.LabelField(MyScript.staticVariable);
DrawDefaultInspector();
}
}
Warning: i did not test this :P.
Aand thanks again. For me nothing is really easy though. I am too used to get trapped by every bug and trap that is available. I get a warning as expected
The name of the script with the static var is " trampolin ". The name of the static var is " triggered ".
I called the script showstaticvar.js. The script is in a folder called Editor, and looks like this now:
@CustomEditor(trampolin)
class MyScriptEditor extends Editor
{
function OnInspectorGUI()
{
EditorGUILayout.LabelField(trampolin.triggered);
DrawDefaultInspector();
}
}
The error message is:
Yes, sorry, try it like this:
@CustomEditor(trampolin)
class MyScriptEditor extends Editor
{
function OnInspectorGUI()
{
EditorGUILayout.LabelField("triggered", trampolin.triggered.ToString());
DrawDefaultInspector();
}
}
This one is tested and it appears to work.
Thanks a bunch Tomvds
The only thing i had to fix was the name of the class. My script is called showstaticvar. And Unity moaned about MyScriptEditor then. I`ve changed that one to showstaticvar now too, and now it works like charm