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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Resolved NetworkVariable<bool> causes InvalidCastException: Specified cast is not valid

Discussion in 'Multiplayer' started by Bechmann, Nov 13, 2023.

  1. Bechmann

    Bechmann

    Joined:
    Nov 5, 2017
    Posts:
    27
    I noticed that one of my prefabs was generating invalid cast exceptions when selected in the editor, out of runtime. I made an empty game object and copied the script that generated the error into a new test script. I narrowed it down and found that it was NetworkVariable<bool> that causes this. So, I deleted everything else in the script, so this is now the only line in the script: "public NetworkVariable<bool> test = new NetworkVariable<bool>();" but it still generates the error in the editor. The variable works fine at runtime. What is going on? Is this an error in the editor itself because I really can't see what should be wrong with this?

    InvalidCastException: Specified cast is not valid.
    Unity.Netcode.Editor.NetworkBehaviourEditor.RenderNetworkContainerValueTypeIEquatable[T] (System.Int32 index) (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.6.0/Editor/NetworkBehaviourEditor.cs:146)
    System.Reflection.RuntimeMethodInfo.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at <834b2ded5dad441e8c7a4287897d63c7>:0)
    Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation.
    System.Reflection.RuntimeMethodInfo.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at <834b2ded5dad441e8c7a4287897d63c7>:0)
    System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) (at <834b2ded5dad441e8c7a4287897d63c7>:0)
    Unity.Netcode.Editor.NetworkBehaviourEditor.RenderNetworkVariable (System.Int32 index) (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.6.0/Editor/NetworkBehaviourEditor.cs:102)
    Unity.Netcode.Editor.NetworkBehaviourEditor.OnInspectorGUI () (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.6.0/Editor/NetworkBehaviourEditor.cs:268)
    UnityEditor.UIElements.InspectorElement+<>c__DisplayClass72_0.<CreateInspectorElementUsingIMGUI>b__0 () (at <53ddbed73faf4fe3b980a493ab4e6639>:0)
    UnityEngine.GUIUtility: ProcessEvent(Int32, IntPtr, Boolean&)
     
    Last edited: Nov 13, 2023
  2. Bechmann

    Bechmann

    Joined:
    Nov 5, 2017
    Posts:
    27
    So, it seems that making the variable public is the cause. Don't really get why, is this not allowed? I'm new to Netcode
     
  3. Bechmann

    Bechmann

    Joined:
    Nov 5, 2017
    Posts:
    27
    Using [HideInInspector] allows me to keep the variable public and prevents the editor from throwing any errors. This can't be intended behavior, right?
     
  4. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    612
    No it's a bug in NGO 1.6, updating to 1.7 should fix it.
     
    Bechmann likes this.
  5. Bechmann

    Bechmann

    Joined:
    Nov 5, 2017
    Posts:
    27
    Thanks, I'll try updating.