Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to get the current player object

Discussion in 'Multiplayer' started by Lexeon, May 21, 2016.

  1. Lexeon

    Lexeon

    Joined:
    Apr 22, 2016
    Posts:
    23
    I've looked everywhere, but to no avail.

    What I want to do is set some data that will be saved on my player object. I have a player object that is instantiated by an overridden method OnServerAddPlayer() method within my NetworkManager class. I want to edit the player data within my overridden OnClientConnect() method within the NetworkManager class, however I cannot get the player gameobject. This is because OnServerAddPlayer() is called only on the server. This means that only the server can get player's gameobject since it is instantiated there.

    The reason I cannot let the server set the data is because the data is retrieved by the client from an external server. I've tried using the NetworkConnection object that is passed in as a parameter of OnClientConnect(), but the clientOwnedObjects hashset is null.

    Here is an example of my NetworkManager class.
    http://pastebin.com/PmaLhyXe
     
  2. Lexeon

    Lexeon

    Joined:
    Apr 22, 2016
    Posts:
    23
    I have found a workaround to this by setting a tag on my object that has the saved player data and having the instantiated player look up the player data object with the tag in the overridden OnStartClient() method. It works now, however I believe I could have a use for the solution of my original method.