简体   繁体   English

Unity Mirror Networking InvalidCastException:指定的演员表无效

[英]Unity Mirror Networking InvalidCastException: Specified cast is not valid

I was trying to create a custom player spawner but keep running into the我试图创建一个自定义玩家生成器,但一直遇到

"Specified cast is not valid "指定的演员表无效

tried searching it online but all results were useless so hope someone here can help me.尝试在网上搜索,但所有结果都没有用,所以希望这里有人可以帮助我。

The problem line:问题行:

NetworkServer.AddPlayerForConnection(conn, playerobject); 

....is were the error occurs. ....是发生错误。 (Full error below) (以下为完整错误)

Code:代码:

public override void OnClientConnect(NetworkConnection conn)
{
    var connection = conn;
    connlist.Add(connection);

    if (!clientLoadedScene)
    {
        if (!NetworkClient.ready) NetworkClient.Ready();
        {
            GameObject playerobject = (GameObject)Instantiate(Player, new Vector3(0, 0, 0), new Quaternion(0, 0, 0, 0));
            playerobject.GetComponent<PlayerManager>().PlayerName = nameplayer;
            NetworkServer.AddPlayerForConnection(conn, playerobject);
            Debug.Log(nameplayer + " is spawned!");
        }
    }

    base.OnClientConnect(conn);
}

Error Full:错误满:

InvalidCastException: Specified cast is not valid. InvalidCastException: 指定的强制转换无效。 Mirror.NetworkIdentity.SetClientOwner (Mirror.NetworkConnection conn) (at Assets/Mirror/Runtime/NetworkIdentity.cs:250) Mirror.NetworkServer.AddPlayerForConnection (Mirror.NetworkConnection conn, UnityEngine.GameObject player) (at Assets/Mirror/Runtime/NetworkServer.cs:617) NetManager.OnClientConnect (Mirror.NetworkConnection conn) (at Assets/Scripts/NetManager.cs:58) Mirror.NetworkManager.OnClientAuthenticated (Mirror.NetworkConnection conn) (at Assets/Mirror/Runtime/NetworkManager.cs:1158) Mirror.NetworkManager.OnClientConnectInternal () (at Assets/Mirror/Runtime/NetworkManager.cs:1142) Mirror.LocalConnectionToServer.Update () (at Assets/Mirror/Runtime/LocalConnections.cs:84) Mirror.NetworkClient.NetworkLateUpdate () (at Assets/Mirror/Runtime/NetworkClient.cs:1249) Mirror.NetworkLoop.NetworkLateUpdate () (at Assets/Mirror/Runtime/NetworkLoop.cs:189) Mirror.NetworkIdentity.SetClientOwner (Mirror.NetworkConnection conn) (at Assets/Mirror/Runtime/NetworkIdentity.cs:250) Mirror.NetworkServer.AddPlayerForConnection (Mirror.NetworkConnection conn, UnityEngine.GameObject player) (at Assets/Mirror/Runtime/NetworkServer .cs:617) NetManager.OnClientConnect (Mirror.NetworkConnection conn) (at Assets/Scripts/NetManager.cs:58) Mirror.NetworkManager.OnClientAuthenticated (Mirror.NetworkConnection conn) (at Assets/Mirror/Runtime/NetworkManager.cs:1158 ) Mirror.NetworkManager.OnClientConnectInternal() (at Assets/Mirror/Runtime/NetworkManager.cs:1142) Mirror.LocalConnectionToServer.Update() (at Assets/Mirror/Runtime/LocalConnections.cs:84) Mirror.NetworkClient.NetworkLateUpdate() (在 Assets/Mirror/Runtime/NetworkClient.cs:1249)Mirror.NetworkLoop.NetworkLateUpdate () (在 Assets/Mirror/Runtime/NetworkLoop.cs:189)

I'm not exactly sure which line you're getting the error on but from what I can remember the Instantiate() method returns a GameObject so you wouldn't need to cast it, additionally you shouldn't be storing your connection var in a list if you intend to keep it, your revised code should be something like:我不确定您在哪一行遇到错误,但我记得Instantiate()方法返回了一个GameObject因此您不需要GameObject转换它,另外您不应该将连接var存储在一个列表,如果你打算保留它,你修改后的代码应该是这样的:

public override void OnClientConnect(NetworkConnection conn)
{
    connlist.Add(conn);

    if (!clientLoadedScene)
    {
        if (!NetworkClient.ready) NetworkClient.Ready();
        {
            var playerobject = Instantiate(Player, new Vector3(0, 0, 0), new Quaternion(0, 0, 0, 0));
            playerobject.GetComponent<PlayerManager>().PlayerName = nameplayer;
            NetworkServer.AddPlayerForConnection(conn, playerobject);
            Debug.Log(nameplayer + " is spawned!");
        }
    }

    base.OnClientConnect(conn);
}

我找到了一个解决方案,我将我的 Spawn 代码移到了一个函数,即 OnServerAddPlayer 而不是 OnClientConnect 它修复了所有问题并且效果惊人!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 InvalidCastException:指定的强制转换在 Unity 中无效 - InvalidCastException: Specified cast is not valid in Unity InvalidCastException: 指定的强制转换无效 || 统一 - InvalidCastException: Specified cast is not valid || Unity InvalidCastException:指定的强制转换无效 - InvalidCastException: Specified cast is not valid 指定的转换无效-System.InvalidCastException - Specified cast is not valid - System.InvalidCastException System.InvalidCastException:指定的强制转换无效 - System.InvalidCastException: Specified cast is not valid InvalidCastException:指定的强制转换在LINQ查询中无效 - InvalidCastException: Specified cast is not valid in LINQ query “ System.InvalidCastException:指定的转换无效”-DateTime - “System.InvalidCastException: Specified cast is not valid” - DateTime System.InvalidCastException:指定的强制转换在.ExecuteScalar上无效 - System.InvalidCastException: Specified cast is not valid at .ExecuteScalar System.InvalidCastException:&#39;指定的强制转换无效。 - System.InvalidCastException: 'Specified cast is not valid.' InvalidCastException:指定的强制转换无效。 (包装器castclass)Unity中的System.Object.__castclass_with_cache(object,intptr,intptr) - InvalidCastException: Specified cast is not valid. (wrapper castclass) System.Object.__castclass_with_cache(object,intptr,intptr) in Unity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM