0
\$\begingroup\$

U hmm I'm new to mirror networking I just wanna find objects that the local player has authority, but it doesn't work on client, is there a solution to this? Well I could make an object tracking script but that will complicate things. This question might look like I haven't research anything, truth is I've searched everywhere there is just no documentation for this

\$\endgroup\$
0
0
\$\begingroup\$

To check if a client has authority or not, as long as the script is from NetworkBehaviour you can just use hasAuthority

void Start() {
    if (hasAuthority)
    {
        //only execute on client with Authority
    }
}

By default almost all objects have server authority. The player prefab assigned in the Networkmanager is the exception. To give a client authority, you need to assign it.

GameObject go = Instantiate(prefab);
NetworkServer.Spawn(go, connectionToClient);

or if it is already spawned

identity.AssignClientAuthority(conn);

If you need to find out who owns an object, you can find out on the server with the identity.connectionToClient property. If it is null then the server has authority.

If you do not need authority over an object, you can add the ignore flag in the command. docu

[Command(ignoreAuthority = true)]
public void CmdRideVehicle() {
    if (player.hasKeys()) {
    //transfer ownership to player  
    }
} 

You can find it in the documentation under authority

\$\endgroup\$
3
  • \$\begingroup\$ hmm that helps, but is there an alternative to clientownedobjects?. simplifying this really helps as i'm actually not making the game from scratch, i'm converting an already complex game to make a multiplayer version. my game is GTA like, and i need to give authority to player to ride vehicles, and for them to exhange vehicle keys back and forth, it will be great if i can ignore authority, but [command(ignoreAuthority = true)] doesn't exist for some reason. i haven't modified any of the script from mirror, is this a problem with unity? i'm using 2020.2 something. \$\endgroup\$ Mar 1 at 12:58
  • \$\begingroup\$ oh thanks, i've figure it out, i ran [Command] and get the clientownedobject in server as it only works there and i used target rpc to give the data back to client, i just learned command and rpc today :) sorry for being an annoying beginner. \$\endgroup\$ Mar 1 at 13:44
  • \$\begingroup\$ @henrybluethrottleunedited there are always multiple ways how to solve it. If it solved your question, you can mark the answer, if you have a better solution (or a more fitting one), feel free to post an answer yourself. If in the future others are stumbling onto the question, they have a solution as well. \$\endgroup\$
    – Zibelas
    Mar 1 at 13:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.