Chaning syncvar on remote object from a child.

So I have a root object with Network Identitiy and a health script. The healthscript contains a syncvar of the type float that is named health.

I then have a sword that is a child object. It contains a Sword script. The Sword script needs to apply damage to a remote player.
I am facing difficulties since the Sword script is a child so it cannot call any commands due to Authority issues.
Any clues?

The work-around I did is the object has a reference to a script attached to the player that contains all commands, and calls it.

I understand what you mean. But cannot get t working. Can you provide a example.

Certainly. The below example assumes that the sword is a child of the player object.
On the sword:

public playerCommands commands;
void Start()
{
    commands = transform.parent.GetComponent<playerCommands>();
    //Whatever else you want the sword to do upon initialization
}

void OnTriggerEnter(Collider collision) //Or where ever you're doing the damaging
{
    if(collision.gameObject.tag == "Player" && collision.gameObject != transform.parent.gameObject) //Make sure we've hit a player that is not the parent of the sword
    {
        commands.CmdSwingSword(collision.gameObject);
    }
}

And the commands script:

[Command]
public void CmdSwingSword(gameObject victim)
{
    //You can set up your formula or whatever to determine the damage dealt. It's better that this be done in the command so that its generated on the server. Never trust the client.
    victim.GetComponent<playerStats>().health -= 50; //health is, of course, a SyncVar
    //Additionally, you could do checks here to see if the victim has no health and is dead.
}

Thanks alot. But it still only updates for the host for some reason. It’s super odd.
For the host it updates but not on the clients making the clients out of sync. This is the setup I use atm:

My sword script is like a child child child so I use transform.root instead of transform.parent

I call this: command.Cmd_ApplyDamage(col.transform.root.gameObject, 10); in my sword. command is a reference to this script:
[Command]
public void Cmd_ApplyDamage(GameObject go, float dmg)
{
go.GetComponent().health -= dmg;
}

It’s almost like I am not using a command call. Yet Im not recieving any warnings about the client not having authority. Cause the syncvar is only being updated locally on the Server. Wich becomes a problem when I want to do local checks on clients to see the others health.

Make sure the command object is in the list of “Spawnable Prefabs” on the network manager, and make sure its network identity has local player authority checked

Alternately, you could make the weapon object itself have client authority in addition to the player

But multiple networkidentities are not supported right?

Ehh…in one prefab? No, because the child identities don’t get spawned and so are not active on the network.
Spawn an object and set it as the child of another object with an identity? Perfectly fine. I do it all the time

Well. So you mean I should have a network Identity on my root object wich I already have. And one on my sword object with is a grandchild of the root? That makes two networkIdentities on one object?

This is the warning I get:
The prefab ‘Player’ has multiple NetworkIdentity components. There can only be one NetworkIdentity on a prefab, and it must be on the root object.

This is because the weapon object is a part of my player and is not spawned in and then added. It’s a part from the start.

If the player is one object with a NetworkIdentity, then his weapon can be a child object of the player with its own NetworkIdentity.

No. Only if it would be spawned in and not actually be a part of the object.

I recieve this warning when I try
The prefab ‘Player’ has multiple NetworkIdentity components. There can only be one NetworkIdentity on a prefab, and it must be on the root object.

That’s what I mean by “in one prefab? No” (Darn English and it’s ambiguities…)
Prefabs can only contain up to 1 NetworkIdentity. If, within a prefab, the sword is a child to an object that has a NetworkIdentity, then do not put one on the sword, or you will get odd behavior on other clients.