- Home /
( Schrödinger's boolean?) custom Editor class OnSceneGUI() and OnInspectorGUI() accessing variable problem!!
Hi, I made a custom editor and it behaves weird.
I created something like this :
[CustomEditor(typeof(MyCustomClass))]
public class MyCustomClassEditor : Editor{
bool myBool = false;
void OnInspectorGUI(){
DrawDefaultInspector();
if(myBool){
if(GUILayout.Button("Disable")){
myBool = false;
//DO SOMETHING
}
}else{
if(GUILayout.Button("Enable")){
myBool = true;
//DO SOMETHING
}
}
}
void OnSceneGUI(){
if(myBool){
//DRAW SOMETHING ON THE SCENE WINDOW
}
}
}
this creates a button in the inspector that toggles the boolean myBool. And it affects the way the OnSceneGUI() behaves.
In fact, it worked without any trouble yesterday, but when I come back for work, it does not work anymore. I tried to figure out what causes the problem, I added a debug script
Debug.Log("Inspector myBool : "+myBool); => OnInspectorGUI() Debug.Log("Scene myBool : " +myBool); => OnSceneGUI()
and here is what I have found :
From OnInspectorGUI() the value changes with respect to the button click. But OnSceneGUI() the value is fixed to false. Even if the value from the OnInspectorGUI() is true, OnScenGUI() shows false. and those value exist simultaneously.
What the... is this a Schrödinger's boolean???
I am having exact same problem, did you figure out a solution?
Unfortunately, no. I didn't find the solution..
Declaring as Public makes no difference, in fact I am using a string in my code and setting the string in OnSceneGUI to a value does nothing, I have a Debug.Log in OnInspectorGUI and it puts Null in the console?
Answer by hulahoolgames · Jan 13, 2016 at 01:59 AM
So what I found was, it does not print any statements in the OnSceneGUI until I move my mouse over to the scene view. Once I do that it starts printing the log I have. Then I added a call to SceneView.RepaintAll() and then the log statement inside OnSceneGUI was printed as soon as I clicked the Enable button. Hope this helps!
[CustomEditor(typeof(MyCustomClass))]
public class MyCustomClassEditor : Editor {
bool myBool = false;
public override void OnInspectorGUI() {
DrawDefaultInspector();
if(myBool){
if(GUILayout.Button("Disable")){
myBool = false;
Logger.Log("Disabled. Bool set to false");
}
}else{
if(GUILayout.Button("Enable")){
myBool = true;
Logger.Log("Enabled. Bool set to true");
SceneView.RepaintAll();
}
}
}
void OnSceneGUI(){
if(myBool){
Logger.Log("ONSCENEGUI : Bool is true");
}
}
}
Oh, I did move my mouse over the scene view to refresh the view, I didn't know the method SceneView.RepaintAll(). Thank you for your answer!