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

Editor bug ? InvalidCastException: Specified cast is not valid

Discussion in 'Editor & General Support' started by DubiDuboni, Jul 14, 2019.

  1. DubiDuboni

    DubiDuboni

    Joined:
    Feb 5, 2019
    Posts:
    131
    My unity editor version is : 2018.4.1f1 Personal


    I have a editor script :

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEditor;
    5. using UnityEditorInternal;
    6. using UnityEngine;
    7.  
    8. [CustomEditor(typeof(ConversationTrigger))]
    9. public class ConversationTriggerEditor : Editor
    10. {
    11.     private ConversationTrigger _conversationTrigger;
    12.  
    13.     [SerializeField] private ReorderableList conversationsList;
    14.  
    15.     private SerializedProperty _conversations;
    16.  
    17.     private int _currentlySelectedConversationIndex = -1;
    18.     private int newSize = 0;
    19.     private Vector2 scrollPos;
    20.  
    21.     private readonly Dictionary<string, ReorderableList> _dialoguesListDict = new Dictionary<string, ReorderableList>();
    22.     private readonly Dictionary<string, ReorderableList> _sentencesListDict = new Dictionary<string, ReorderableList>();
    23.  
    24.     private void OnEnable()
    25.     {
    26.         _conversationTrigger = (ConversationTrigger)target;
    27.  
    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 :

    Code (csharp):
    1.  
    2. _conversationTrigger = (ConversationTrigger)target;
    3.  
    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.
     
  2. DubiDuboni

    DubiDuboni

    Joined:
    Feb 5, 2019
    Posts:
    131
    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 ?
     
    vlab22 likes this.
  3. iscf

    iscf

    Joined:
    Aug 30, 2018
    Posts:
    24
    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.
     
    vlab22 likes this.
  4. PiterQ70

    PiterQ70

    Joined:
    Mar 3, 2015
    Posts:
    82
    Weird bug. In my case occurs when unity recompile after script changed.
    Help check if target is null
    Code (CSharp):
    1. if (target == null) return;
     
  5. vlab22

    vlab22

    Joined:
    Nov 22, 2012
    Posts:
    17
    Same behavior here if casting target in the OnEnable.
    Close/Open Unity solved the problem.

    Unity 2020.3.5f1
    Windows 10
     
    Xepherys likes this.
  6. Xepherys

    Xepherys

    Joined:
    Sep 9, 2012
    Posts:
    204
    Yeah, I was having this issue as well, and just moved the assignment of my target into CreateInspectorGUI().
     
  7. Yany

    Yany

    Joined:
    May 24, 2013
    Posts:
    93
    Same issue here:
    Unity v2022.2.14f1
    Windows 11, updated.

    The exception has gone after the editor restarted.
     
  8. Disgrabled

    Disgrabled

    Joined:
    May 25, 2023
    Posts:
    1
    Unity 2021.3.20f1
    Windows 11.
    The exception error gone after editor restarted as well.