I need help with SyncVar.
My problem is that the SyncVar won't update on new clients. Here is the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class ChangeCharacter : NetworkBehaviour
{
[SyncVar(hook = "OnChangeCharacter")]
public string character;
public GameObject GM;
public GameObject BM;
public GameObject GUI;
public void Start()
{
character = "boy";
}
[ClientRpc]
public void RpcOnCharacterChanged(string newcharacter)
{
character = newcharacter;
if (!isLocalPlayer)
{
return;
}
DetermineCharacter();
}
[Command]
void CmdTellServerMyCharacter(string newcharacter)
{
RpcOnCharacterChanged(newcharacter);
}
public void OnClickButton()
{
CmdTellServerMyCharacter(character);
}
void OnChangeCharacter(string newValue)
{
character = newValue;
DetermineCharacter();
}
void DetermineCharacter()
{
if (character == "boy")
{
BM.SetActive(true);
GM.SetActive(false);
}
else
if (character == "girl")
{
BM.SetActive(false);
GM.SetActive(true);
}
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612214606im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
uNet rotation syncing problem 0 Answers
Sync complex variables and components over network 1 Answer
Unity Mirror OnStartServer spawn GameObjects 1 Answer
Unity Network (Player) 1 Answer