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

AssignClientAuthority for [gameObject] owner cannot be null

Discussion in 'Multiplayer' started by Gelbrekt, Aug 8, 2018.

  1. Gelbrekt

    Gelbrekt

    Joined:
    Jul 14, 2017
    Posts:
    5
    The code below works for instantiating an object across the server and all clients in a multiplayer game, but only if put in the Player Prefab in the Network Manager (gameObject).

    Code (CSharp):
    1.  [Command]
    2. void CmdSimpleCreate()
    3. {
    4.      GameObject theObject = Instantiate(objectThatIsToBeCreated);
    5.      NetworkServer.Spawn(theObject);//Online instantiate
    6.      globalStorageVariable = theObject;//Saves the gameObject in a global variable so that it can be used outside of the function. This variable is a SyncVar.
    7.      globalGameObject.GetComponent<NetworkIdentity>().AssignClientAuthority(connectionToClient);
    8. }
    When put in another gameobject, it returns the following error even when the player “hasAuthority” over that object.
    "AssignClientAuthority for Missile A(Clone) (UnityEngine.GameObject) owner cannot be null. Use RemoveClientAuthority() instead."

    Apparently the player that “hasAuthority” over the object is not its owner?
    I imagine this can be fixed with a single line of code that sets the objects owner, but I have not managed to find it. Please help, I have been stuck on this for days!
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Your problem I believe is your use of connectionToClient, as it only works on the special Player GameObject. See its description.

    https://docs.unity3d.com/ScriptReference/Networking.NetworkIdentity-connectionToClient.html
     
  3. Gelbrekt

    Gelbrekt

    Joined:
    Jul 14, 2017
    Posts:
    5
    Thank you very much for the answer. You are clearly right, but I am still uncertain. I think Unity's documentation on this is extremely thin and i struggle to find other sources.

    It says on this link that the Player Prefab is "the default prefab to be used to create player objects on the server.". Surely this means that it is possible to create multiple prefabs that can do this, but i cannot see how. Is it perhaps possible to just add a different argument into the AssignClientAuthority() method?

    How did you learn how to spawn multiple objects? I have watched over 5 tutourial series on youtube but none of them answers this question. Would be very happy if you could give an answer!
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Personally, I avoid assigning client authority to anything. It overly complicates things. So I run with all objects with server authority, and when I need to manipulate the object from a client I send a Command to the server to tell the server to do it. I place the Command on the Player GameObject. If doing so would be somehow problematic, I send a Unet Message instead.

    Though with the announced deprecation and impending removal of Unet from Unity, I'm actually in the process of replacing Unet in my project entirely. I'd suggest you do the same unless you expect to release the game soon.
     
  5. Gelbrekt

    Gelbrekt

    Joined:
    Jul 14, 2017
    Posts:
    5
    Ah, I suspected that the reason why the documentation is bad is because Unet is getting replaced. Do you think it is worth to keep learning about the basics of Unity 5 networking at all? I heard that the new multiplayer in Unity 2017/18 is very different.
     
  6. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    If your game is nearly complete and you're just working on bug fixes and polish with a release soon, I'd stick with it. Otherwise I'd move to an alternative networking solution. Unity 2019 will not have Unet included. The new networking options Unity is working on have had very few details revealed, so I wouldn't plan to use them in a current project at this time.
     
  7. Gelbrekt

    Gelbrekt

    Joined:
    Jul 14, 2017
    Posts:
    5
    Alright, but what alternative is there then? I thought Unet was the only alternative. Are you refering to networking solutions that are not made by unity?
     
  8. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Yes I'm referring to 3rd party solutions. Photon is probably the most well known, but they aren't the only option. There is also C#'s built in socket interface to roll your own starting from a low level.
     
  9. Gelbrekt

    Gelbrekt

    Joined:
    Jul 14, 2017
    Posts:
    5
    Ok, I see, thank you for the advice!