0

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. 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)

2 Answers 2

1

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:

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);
}
1
  • Yea that was just after i got the bug I was trying a lot I didn't remove it yet but The AddPlayerForConnection is the problem Commented Nov 5, 2021 at 9:31
0

I found a solution i moved my Spawn code to a function that is OnServerAddPlayer instead of OnClientConnect it fixes all the problems and works amazing!

Not the answer you're looking for? Browse other questions tagged or ask your own question.