Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question "NullReferenceException UnityEditor.SerializedObject..ctor" error when trying to create an editor

Discussion in 'Scripting' started by Ozgekocaoglu, Dec 8, 2022.

  1. Ozgekocaoglu

    Ozgekocaoglu

    Joined:
    Feb 13, 2019
    Posts:
    38
    Hi everyone,

    When i compile my script, Unity throws this error to me. I guess it is because of the Editor.Create function. But i'm sure.
    How can i fix that?

    Here is my code:

    Code (CSharp):
    1.   // -------------------------------------------------------------------------
    2.   protected override Editor createBaseEditor()
    3.   {
    4.     return Editor.CreateEditor(targets, Type.GetType("UnityEditor.TransformInspector, UnityEditor, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"));
    5.   }
    And here is the errror code::

    Code (csharp):
    1. SerializedObjectNotCreatableException: Object at index 0 is null
    2. UnityEditor.Editor.CreateSerializedObject () (at <68089899e4c84456bfc1de3436accf4a>:0)
    3. UnityEditor.Editor.GetSerializedObjectInternal () (at <68089899e4c84456bfc1de3436accf4a>:0)
    4. UnityEditor.Editor.get_serializedObject () (at <68089899e4c84456bfc1de3436accf4a>:0)
    5. UnityEditor.TransformInspector.OnEnable () (at <68089899e4c84456bfc1de3436accf4a>:0)
     
  2. tomfulghum

    tomfulghum

    Joined:
    May 8, 2017
    Posts:
    69
    From the error message I'm guessing the element at index 0 of the targets array is null for whatever reason. CreateEditor doesn't accept targets that are null, so you'll have to fix that.
     
  3. Ozgekocaoglu

    Ozgekocaoglu

    Joined:
    Feb 13, 2019
    Posts:
    38
    But my target array coming from "Editor.targets" and Unity says:

    Code (CSharp):
    1.         // Summary:
    2.         //     An array of all the object being inspected.
    3.         public UnityEngine.Object[] targets { get; }
    4.         //