Editor bug ? InvalidCastException: Specified cast is not valid

My unity editor version is : 2018.4.1f1 Personal

I have a editor script :

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;

[CustomEditor(typeof(ConversationTrigger))]
public class ConversationTriggerEditor : Editor
{
    private ConversationTrigger _conversationTrigger;

    [SerializeField] private ReorderableList conversationsList;

    private SerializedProperty _conversations;

    private int _currentlySelectedConversationIndex = -1;
    private int newSize = 0;
    private Vector2 scrollPos;

    private readonly Dictionary<string, ReorderableList> _dialoguesListDict = new Dictionary<string, ReorderableList>();
    private readonly Dictionary<string, ReorderableList> _sentencesListDict = new Dictionary<string, ReorderableList>();

    private void OnEnable()
    {
        _conversationTrigger = (ConversationTrigger)target;

The variable _conversationTrigger is null and also target is “null”

They are both null when running the game but then when the game is continue the game is working fine without any problems. And both are not null again.

The exception message in the editor is :

InvalidCastException: Specified cast is not valid. On line 25.
Line 25 is :

_conversationTrigger = (ConversationTrigger)target;

When using a break point on the target I see :

cloneDestroyedMessage : “Instantiate failed because the clone was destroyed during creation. This can happen if DestroyImmediate is called in MonoBehaviour.Awake.” And objectisNullMessage : “The Object you want to instantiate is null.”

I’m not using any DestroyImmediate at any place in my project.

And :

UnityEngine.MissingReferenceException: The variable m_Targets of ConversationTriggerEditor doesn’t exist anymore. You probably need to reassign the m_Targets variable of the ‘ConversationTriggerEditor’ script in the inspector.

I checked many times everything is assigned in the editor.
The fact is that after few seconds they are not null and everything in the game is working fine.

Seems like a bug in the editor.

I just closed my project the editor and re opened it and now the exception is gone not show.
Spent some times to figure it out. Is that a known bug ?

1 Like

Same here, I close and reopen my editor and the exception is gone. Seems like some bug when renaming a class with a custom inpector attached, Unity maybe don’t update the intern structure very well.

1 Like

Weird bug. In my case occurs when unity recompile after script changed.
Help check if target is null

if (target == null) return;

Same behavior here if casting target in the OnEnable.
Close/Open Unity solved the problem.

Unity 2020.3.5f1
Windows 10

1 Like

Yeah, I was having this issue as well, and just moved the assignment of my target into CreateInspectorGUI().

Same issue here:
Unity v2022.2.14f1
Windows 11, updated.

The exception has gone after the editor restarted.

Unity 2021.3.20f1
Windows 11.
The exception error gone after editor restarted as well.

也许是因为你使用了嵌套的类型,或者简单地说,脚本名称与脚本类型不一致;解决方案就是保证脚本名称和脚本内的类型保持一致。不要使用嵌套类型

Perhaps because you are using nested types, or simply put, the script name does not match the script type; the solution is to ensure that the script name and the type within the script remain consistent. Do not use nested types.