- Home /
DisplayCancelableProgressBar NullReferenceException
I'm having trouble getting the progress bar to work. It does display and does show the progress, but it also results in a null reference exception that I can't figure out.
In my editor script during OnInspectorGUI, I'm calling this several times:
EditorUtility.DisplayProgressBar("My Task", "Operation...", progress);
At some point during execution, this error shows up:
NullReferenceException: Object reference not set to an instance of an object UnityEngine.GUIUtility.GetControlID (Int32 hint, FocusType focus) UnityEditor.InspectorWindow.OnGUI () System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr,
System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture)
Any ideas what could be wrong or what's missing? At this point I'm ready to give up and use some alternate method of showing progress.
Answer by watsonsong · Mar 31, 2017 at 05:20 AM
I met the same problem, when using the DisplayCancelableProgressBar. If i remove it, there is no null exception throw any more.
Answer by rgrandguillot · Mar 20, 2018 at 09:58 PM
I came across the same issue after upgrading to Unity 2017.3. Before that, with Unity 5.6, DisplayCancelableProgressBar was acting perfectly with no error.
Have you filed a bug report for Unity? I remember having a similar issue, but forgot about it, so I doubt Unity is aware of it.
I never had any problems with it. However the example shown in the documentaion doesn't make too much sense. THe DisplayCancelableProgressBar is meant to be used in a blocking execution. It's basically a modal window. If the process is started from within a OnGUI block you may want to call ExitGUI once the process has completed. So a typical usecase would be
void OnGUI()
{
if (GUILayout.Button("Do heavy work"))
{
for(int i = 0; i < 100000; i++)
{
if (i%100 == 0 && EditorUtility.DisplayCancelableProgressBar("", "", (float)i/100000))
break;
// do some heavy work here
System.Thread.Sleep(10);
}
EditorUtility.ClearProgressBar();
EditorGUIUtility.ExitGUI();
}
}
So when you press the "Do heavy work" button you basically block the main thread until your work is done. The CancelableProgressBar allows the user to interrupt that process.
Fantastic! The EditorGUIUtility.ExitGUI(); solves the problem. Thanks a lot.