As you can see above, the host correctly displays this information, the client works but does not display the information to the server. I am so confused now I feel I have tried everything. Here is my code please any help is appreciated!
This is attached on Player:
[SyncVar(hook = "Rpcchangechest")]
public int echest;
[ClientRpc]
public void Rpcchangechest(int num) {
echest = num;
if (body.male) {
if (ChestName == ChestName) {
Destroy (ChestInst);
}
body.Mesh_male_torso.active = false;
body.Mesh_male_arms.active = true;
ChestName = Chest [num].name;
GameObject NewChest = (GameObject)Instantiate (Resources.Load ("Armor/" + ChestName));
ChestInst = NewChest;
Destroy (NewChest);
AddChest ();
Cmdloadarmor();
}
}
This is from my UI Script.
if (itemInfo.EquipType == UIEquipmentType.Chest && !equipped)
{
//for calling on client
transform.root.SendMessage("Rpcchangechest", itemInfo.ID);
//Supposed to sync?
Player.GetComponent<ChangeArmor>().echest = itemInfo.ID;
}
ClientRpc and SyncVar are only synced if they are called from the server. You need to add a command method which is executed on the server.
Add this to the player script
[command]
public void Cmdchangechest (int num)
{
echest = num
}
Then do this in the UI script
if (itemInfo.EquipType == UIEquipmentType.Chest && !equipped)
{
//for calling on client
Player.GetComponent<ChangeArmor>().Cmdchangechest = itemInfo.ID;
}
After a very long two days I figured out my first problem that UNET has attacked me with. I will show my solution for anyone else needing help.
From my UI script that was attached in my Player object a command was sent to use this function(Which will find out if the player is the host, or not the host):
public void updatechest(int newid)
{
if (!Network.isServer)
{
Cmdchangechest(newid);
} else
{
changechest(newid);
}
}
Next I have my armor script attached to my Player, with a VarSync variable that calls the hook whenever the eChest variable is changed (We want to only change it with the server, using [Command])
[SyncVar(hook = "changechest")]
public int echest;
Then I had the server use this command, I figured any other change of your variable can cause errors, so this is the only time in my whole piece of code I change eChest:
[Command]
void Cmdchangechest(int num)
{
echest = num;
}
So sure enough, everything started working correctly. Having [ClientRPC] on the hook function will make things complicated and atleast for me messed it all up. Im guessing perfection is a huge play here within UNET and this has been so dang confusing… But success!