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

How to give a client-spawned object localplayerAuthority?

Discussion in 'Multiplayer' started by CarterG81, Jul 1, 2015.

  1. CarterG81

    CarterG81

    Joined:
    Jul 25, 2013
    Posts:
    1,773
    If I have a client send a command to spawn a prefab, how do I give that spawned object player authority so the client can control it?
     
  2. Vanamerax

    Vanamerax

    Joined:
    Jan 12, 2012
    Posts:
    938
    You can either check the localAuthority checkbox on the networkIdentity on your prefab, assuming you dont have a player object yet. Note that the networkManager has a PlayerPrefab field that will handle spawning the player object automatically on connect.

    If you do have a player object already, use the NetworkServer.ReplacePlayerForConnection function to replace the current playerObject with the new prefab. Make sure the localAuthority field is checked on the prefab and the prefab is marked spawnable on the client. Also note that the old object wont be destroyed. You will need to do that manually.
     
  3. CarterG81

    CarterG81

    Joined:
    Jul 25, 2013
    Posts:
    1,773
    Ahhh, okay. I thought this was the only way (hoping for a different way) but that was only because I was under the assumption it "replaced" the player (destroyed it when adding the new one). Why is this even called "Replace" then, if it does not replace but simply Adds?

    If the old object isn't destroyed- then perfect!
     
    Last edited: Jul 2, 2015
  4. Vanamerax

    Vanamerax

    Joined:
    Jan 12, 2012
    Posts:
    938
    It will not be destroyed, but the local authority for the client will be revoked. At least that is what I am understanding from the manual
     
  5. CarterG81

    CarterG81

    Joined:
    Jul 25, 2013
    Posts:
    1,773
    If it replaces the localauthority of the old gameobject, then that isn't a solution for me.

    I need to be able to spawn a new gameobject and give it localauthority without REPLACING the original playerPrefab.