-1

I call the PrefabUtility.ReplacePrefab in OnInspectorGUI function ,but it is error. the gameobject is exit. the error is NullReferenceException: Object reference not set to an instance of an object UnityEditor.InspectorWindow.GetEditorsWithPreviews (UnityEditor.Editor[] editors) UnityEditor.InspectorWindow.DrawPreviewAndLabels ()

please how to deal with this problem?

lsonvoew
  • 29
  • 1
  • Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Remy Aug 04 '20 at 06:16
  • Please show your complete code. Otherwise in particular for editor scripts it is extremely hard to tell what exactly happens... – derHugo Aug 04 '20 at 06:34

1 Answers1

0

the code :

public override void OnInspectorGUI()
{
            if (null == this.myTarget)
            {
                return;
            }
            PrefabType prefabType = PrefabUtility.GetPrefabType(mCurEditScene);
            if (prefabType == PrefabType.PrefabInstance ||
                prefabType == PrefabType.DisconnectedPrefabInstance)
            {
                GameObject rootUploadGameObject = PrefabUtility.FindValidUploadPrefabInstanceRoot(mCurEditScene.gameObject);
                var prefabParent = PrefabUtility.GetPrefabParent(rootUploadGameObject);
                PrefabUtility.ReplacePrefab(rootUploadGameObject, prefabParent, ReplacePrefabOptions.ConnectToPrefab);
            }
 }

errors :

NullReferenceException: Object reference not set to an instance of an object
UnityEditor.InspectorWindow.GetEditorsWithPreviews (UnityEditor.Editor[] editors)
UnityEditor.InspectorWindow.DrawPreviewAndLabels ()
UnityEditor.InspectorWindow.OnGUI ()
System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:222)
Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation.
System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:232)
System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MethodBase.cs:115)
UnityEditor.HostView.Invoke (System.String methodName, System.Object obj) 
UnityEditor.HostView.Invoke (System.String methodName)
UnityEditor.HostView.InvokeOnGUI (Rect onGUIPosition)
lsonvoew
  • 29
  • 1