1

I am making a multiplayer game and I want to have player to interact with a non-player object (whose transform can be changed by any player). When I interact them with the player who joined first (or the guy who is hosting) its working but if I try to interact it with the another player (the one who joined second) the objects goes back to the location that the first player left him at.

So what I tried is to shift the authority of non-player object but I am having the following errors. Anyone is having the same issue or knows any other way to do the above task? I am using the following code to change the authority:

    [Command]
    void Cmd_AssignLocalAuthority(GameObject obj)
    {
        print("shifting authority successfully");
        NetworkInstanceId nIns = obj.GetComponent<NetworkIdentity>().netId;
        GameObject client = NetworkServer.FindLocalObject(nIns);
        NetworkIdentity ni = client.GetComponent<NetworkIdentity>();
        ni.AssignClientAuthority(connectionToClient);
    }

    [Command]
    void Cmd_RemoveLocalAuthority(GameObject obj)
    {
        print("reverting authority successfully");
        NetworkInstanceId nIns = obj.GetComponent<NetworkIdentity>().netId;
        GameObject client = NetworkServer.FindLocalObject(nIns);
        NetworkIdentity ni = client.GetComponent<NetworkIdentity>();
        ni.RemoveClientAuthority(ni.clientAuthorityOwner);
    }

And the error I am getting is this

enter image description here

Dr Rob Lang
  • 6,659
  • 5
  • 40
  • 60

1 Answers1

1

You need to know that the changes SHOULD be called from a player object, not the object itself, as it do not have authority.

For setting authority you should do something like this:

    [Command]
    public void CmdSetAuth(NetworkInstanceId objectId, NetworkIdentity player)
    {
        GameObject iObject = NetworkServer.FindLocalObject(objectId);
        NetworkIdentity networkIdentity = iObject.GetComponent<NetworkIdentity>();

        //Checks if anyone else has authority and removes it and lastly gives the authority to the player who interacts with object
        NetworkConnection otherOwner = networkIdentity.clientAuthorityOwner;
        if (otherOwner == player.connectionToClient)
        {
            return;
        }
        else
        {
            if (otherOwner != null)
            {
                networkIdentity.RemoveClientAuthority(otherOwner);
            }
            networkIdentity.AssignClientAuthority(player.connectionToClient);
        }

        networkIdentity.AssignClientAuthority(player.connectionToClient);
    }
Dr Rob Lang
  • 6,659
  • 5
  • 40
  • 60
Lotan
  • 4,078
  • 1
  • 12
  • 30
  • let me give it a try ! So you mean I have to attach this script with the Player object in my game ? – Haseeb Asif Oct 20 '18 at 21:36
  • Hy man ! thanks its working quite fine but now lies the problem when i transfer authority to client i also now want the authority back to server client , And thats not working now ! sharing the code written so may be you help me here also – Haseeb Asif Oct 20 '18 at 21:59
  • here is the link to code ! sorry i was unable to post code in comments as i dont know how to do it ! https://mega.nz/#!B3gWDKzB!G0CY_BVH97BwZlZIPKvkbZOQeA7oZt9R_DOvX873E60 – Haseeb Asif Oct 20 '18 at 22:17
  • I am also sharing the git project here ! So you can have a look – Haseeb Asif Oct 20 '18 at 22:46
  • here is git link https://bitbucket.org/haseebasif/unet-box-movement/src/master/ thanks in advance – Haseeb Asif Oct 20 '18 at 22:57