Search Unity

Accessing Server from Client in native networking

Discussion in 'Multiplayer' started by BlackPanda, Mar 10, 2015.

  1. BlackPanda

    BlackPanda

    Joined:
    Jan 24, 2014
    Posts:
    78
    Hi I was doing the following:

    Code (CSharp):
    1. if (Network.isServer)
    2. {
    3.     playerServer = networkView.owner;
    4.     playerClient = Network.connections[0];
    5. }
    6. else
    7. {
    8.     // ERROR HERE
    9.     playerServer = Network.connections[0];
    10.     playerClient = networkView.owner;
    11. }
    But as it turns out when this code is run on a client, both playerServer and playerClient have the same value. Does Network.connections[0] on a client not return its server?? What I might be doing wrong here??
     
  2. BlackPanda

    BlackPanda

    Joined:
    Jan 24, 2014
    Posts:
    78
    Bump! Anybody knows a way around?? Sorry.
     
  3. gfoot

    gfoot

    Joined:
    Jan 5, 2011
    Posts:
    550
    It probably just means that this network view really is owned by the server. So on the client, networkView.owner is the server, and Network.connections[0] is also the server.

    If you want to get a local NetworkPlayer object on the client, use Network.player:

    http://docs.unity3d.com/ScriptReference/Network-player.html
     
    BlackPanda likes this.
  4. adi7b9

    adi7b9

    Joined:
    Feb 22, 2015
    Posts:
    181
  5. BlackPanda

    BlackPanda

    Joined:
    Jan 24, 2014
    Posts:
    78
    Thank you so much @gfoot. I'll try that.
     
  6. BlackPanda

    BlackPanda

    Joined:
    Jan 24, 2014
    Posts:
    78
    I don't think this is what I'm looking for. I want to get client info from the client itself.
     
  7. adi7b9

    adi7b9

    Joined:
    Feb 22, 2015
    Posts:
    181
    but you're the client.. use:
    Code (CSharp):
    1. Network.player
     
  8. BlackPanda

    BlackPanda

    Joined:
    Jan 24, 2014
    Posts:
    78
    Yes. I solved it. :) Thanks @gfoot it's working. I can't believe i missed a simple thing like this! :confused: