Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

public override void OnServerAddPlayer() Issue

Discussion in 'Multiplayer' started by DRRosen3, Aug 25, 2015.

  1. DRRosen3

    DRRosen3

    Joined:
    Jan 30, 2014
    Posts:
    683
    I'm using a custom network manager class, and for now I'm simply trying to successfully override the OnServerAddPlayer function. The issue I'm running into is that the player isn't being given authority over the GameObject. If in a script attached to the player's GameObject I run a Debug.Log about the NetworkIdentity.isLocalPlayer ... it returns false for the client (and the host for that matter). My custom network manager class is below.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3. using UnityEngine.Networking.Match;
    4.  
    5. public class Network_Manager : NetworkManager
    6. {
    7.  
    8.     public override void OnServerAddPlayer(NetworkConnection _connection, short _playerControllerId)
    9.     {
    10.         GameObject obj;
    11.         Vector3 pos = Vector3.zero; //CODE WILL GO HERE TO GET PLAYER'S LAST STORED VECTOR 3 FROM A DATABASE.
    12.         Quaternion rot = Quaternion.identity;
    13.  
    14.         if(_playerControllerId > 0)
    15.             obj = Resources.Load("Prefabs/Sphere") as GameObject; //TEMPORARILY USING A CUBE AS A PLAYER'S MINION FOR TESTING.
    16.         else
    17.             obj = Resources.Load("Prefabs/Cube") as GameObject; //TEMPORARILY USING A CUBE AS THE PLAYER FOR TESTING.
    18.  
    19.         Instantiate(obj, pos, rot);
    20.  
    21.         NetworkServer.AddPlayerForConnection(_connection, obj, _playerControllerId);
    22.  
    23.         if(_playerControllerId > 0)
    24. //IF THIS IS A MINION THEN WE NEED TO LET THE CLIENT'S PLAYER GameObject BE ASSOCIATED WITH IT.
    25.             _connection.playerControllers[0].gameObject.GetComponent<Test_Input>().Activate(obj.GetComponent<NetworkIdentity>().netId, _connection.connectionId);
    26.     }
    27.  
    28. }
     
  2. DRRosen3

    DRRosen3

    Joined:
    Jan 30, 2014
    Posts:
    683
    BUMP ... nobody willing to help me look into this?
     
  3. Giometric

    Giometric

    Joined:
    Dec 20, 2011
    Posts:
    170
    Just from this snippet, I don't see anything that looks incorrect. Is the player unable to control their player object at all? Where is your Debug.Log that says NetworkIdentity.isLocalPlayer is false? I think on the server, that will be true by the time Start() is run, but on the clients I believe that doesn't become true until at least StartClient is run (I'm assuming Test_Input is a NetworkBehaviour?). Also, make sure that you're registering those prefabs you're grabbing with Resources.Load before you instantiate them. They need to be registered on both the clients and server.
     
  4. Firoball

    Firoball

    Joined:
    Aug 6, 2015
    Posts:
    60
    Did you set LocalPlayerAuthority on the NetworkIdentity of your player/minion prefabs?
     
  5. DRRosen3

    DRRosen3

    Joined:
    Jan 30, 2014
    Posts:
    683
    Correct. They are unable to perform anything authorative on their object.
    I have it in the Update() function.
    I've also tried it this way...
    Code (CSharp):
    1. obj = singleton.playerPrefab as GameObject;
    ...on line 17. Still doesn't work.
    Yes.
    Yes, "Local Player Authority" is checked on the prefab. The issue isn't with this variable of the NetworkIdentity...it's with the "isLocalPlayer" variable. It's not being set to true.
     
  6. BrestenskyMW

    BrestenskyMW

    Joined:
    Apr 16, 2020
    Posts:
    1
    Replace line 19 with:
    val thing = Instantiate(obj, pos, rot);
    Then add:
    NetworkServer.Spawn(thing);