Proper way to update a non-local player object/Possible bug...

Hi,

I am having trouble updating a non-local player object.

Inside my object, I have a function call that I want to be propagated from a client to the server and then to all the clients.

In a script attached to my player object, I have this Command:

[Command]
public void CmdTest () {
    FooBar foo = GameObject.FindObjectOfType<FooBar > ();
    foo.doSomething ();
    foo.RpcdoSomething  ();
}

This seems very inefficient, but it works. The weird part is when I’m inside my FooBar object, I can have a function that calls that command and it still works, but I can’t have that Command inside my FooBar object. Is this expected behavior?

How do I properly call functions on non-local player objects?

Thanks!

I think Commands can only be in something with local authority, like the player object.

In the command you have, you could change a SyncVar (that has a hook function) in the same script. In the hook, which runs on clients, you could make whatever change you want to happen to the player, based on the new value of the SyncVar.

I’m not trying to change the player though. I’m trying to change an object that has server authority by an action that is caused by the player.

if the object has a networkidentity, you can just pass it to the command. But make sure you have validation that this player owns the object.

sing UnityEngine;
using System.Collections;
using UnityEngine.Networking;

public class args : NetworkBehaviour{

    [Command]
    void CmdOther(GameObject other, int value)
    {
        // validation here
        other.GetComponent<args>().RpcStuff(42);
    }

    [ClientRpc]
    void RpcStuff( int value)
    {
            Debug.Log("stuff  " + value + " isLocalPlayer:" + isLocalPlayer);
    }

    void Update ()
    {
        if (Input.GetKeyDown(KeyCode.Space))
            CmdOther(gameObject, 76545);
    }
}