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

[Solved] NetworkBehaviour.isLocalPlayer is return false for both the server and the client.

Discussion in 'Multiplayer' started by asperatology, Aug 4, 2015.

  1. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    Here I have both cubes selected in the server and the client:



    And here's the relevant code:

    Code (CSharp):
    1.     private void Update() {
    2.         InputEvent();
    3.         if (this.selectedObjects.Count > 0) {
    4.             if (this.isLocalPlayer) {
    5.                 SetSelectionColor(this.selectedObjects, Color.green);
    6.             }
    7.             else {
    8.                 SetSelectionColor(this.selectedObjects, Color.magenta);
    9.             }
    10.         }
    11.     }
    If I select one of the cubes in both the server and the client, the selected cube shows up as magenta. It doesn't matter if I select the cube in the server or in the client, the cubes themselves will always show up as magenta and not green.

    The docs mentioned the NetworkBehaviour.isLocalPlayer will return true if the object is represented by the player's local machine. I am guessing the "local machine" part is referring to the instance of the Unity Player. If it represents the machine I am using, then the cubes should be colored in as green.

    This is very confusing. If neither the server nor the client owns the cubes, when who owns it?
     
  2. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    Only player objects use isLocalPlayer. are the cubes players?

    There is hasAuthority for non-player objects
     
  3. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    Yes, the cubes are "players", in a sense that they should be player-controlled.

    And both the server and the clients do not have authority on the cube prefabs auto-spawned in through Network Manager.
     
  4. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    I haven't completely solved this yet.

    The only hint so far that I can try is to make the cube game objects react to the player's inputs by using a mix of [ClientCallback], [SyncVar], and [Command]. It's not pretty, and it's very unstable.

    If anyone else knows about spawned prefabs with no authorities on both the server and clients, please post here.
     
  5. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    It sounds like a bug. Can you post a project that reproduces this?
     
  6. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    Hi there,

    Would you like me to give you the bug report ID number, or have the project be hosted on GitHub, so you can take it away and test?

    I just sent out a bug report, and am currently waiting on the confirmation email, so I can get the ID number.

    I rewrote a simpler version in a clean project (with UnityVS) to replicate the bug, and it's 100% reproducible.
     
  7. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    Bug ID Number is in:
    718435

    Some more testing reveals that isLocalPlayer and hasAuthority toggles to TRUE and FALSE, back and forth, for each Update().

    If you constantly let the game run, the Console will keep spitting is and isn't local player and has authority each respectively. Hm...
     
  8. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    When I run that project, everything seems to be working correctly. The white cube is in the same place as the colored, cube, so you cant see the colored one. If you move the white cube (or spawn the players at different locations), you can see the colored cube.

    The preview panel in the inspector shows the local player and authority status for objects:

    upload_2015-8-8_13-6-45.png

    The logs of alternating authority/not-authority are for different objects, not the same object changing state.
     
  9. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    Ah, that explains how I'm getting white cubes on both the server and the client. It's kind of unexpected how there's no Z-fighting, when both cubes are placed in the same position. And it brought out more questions. Don't worry, I asked them about this here.

    I guess you can close that bug report, seeing how this is resolved.

    Also, I never knew you can inspect the Network Information in the Inspector on the cloned cubes.