- Home /
Mirror : How to Sync child objects active status on join?
Basically I have a player and all the weapons and armour are attached to them and set to inactive. When they pick up something, the code finds the corresponding ID and sets it to active and all others to inactive.
To test this I just use the E button to set an axe on my player to active.
I have this working but the issue is with networking. If the client joins right after the host and then the host presses E. The axe shows up for both and animations are good. Same with the client. But if the host starts and then presses E and the axe shows up, when the client connects the axe does not showing up on their screen for the host. It only shows the default player with nothing set to active.
Here is the code for the relevant parts
void Start()
{
x_axisMovement = 0f;
y_axisMovement = 0f;
z_axisMovement = 0f;
if(playerSpeed == 0){playerSpeed = 5f;}
if(rotationSpeed == 0){rotationSpeed = 0.05f;}
if(isLocalPlayer)
{
isWeaponOut = false;
}
}
[Client]
public void OnEquipOrUnequipWeapon(InputAction.CallbackContext context)
{
if(!isLocalPlayer) {return;}
if(!context.performed) {return;}
int weaponId = 0;
if(isWeaponOut)
{
isWeaponOut = false;
}
else
{
isWeaponOut = true;
}
CmdSetWeaponActive(weaponId, this.gameObject, isWeaponOut);
}
[Command]
public void CmdSetWeaponActive(int weaponId, GameObject playerInstance, bool isWeaponOut)
{
ServerSetWeaponActive(weaponId, playerInstance, isWeaponOut);
}
[ClientRpc]
public void ServerSetWeaponActive(int weaponId, GameObject playerInstance, bool isWeaponOut)
{
Weapons[] listOfWeapons = gameObject.GetComponent<Player>().weaponAnchorLocation.gameObject.GetComponentsInChildren<Weapons>(true);
foreach(Weapons w in listOfWeapons)
{
if (w.GetWeaponId() == weaponId)
{
w.gameObject.SetActive(isWeaponOut);
if(isWeaponOut)
{
activeWeapon = w.gameObject;
}
else
{
activeWeapon = null;
}
}
else
{
w.gameObject.SetActive(false);
}
}
}
I am not sure how to sync the active setting for child objects when a client joins
Your answer
Follow this Question
Related Questions
Command method dont change variables on server 0 Answers
Understanding Unet attributes 2 Answers
Transport Layer WrongHost 1 Answer
Rpc client not functioning as expected (Mirror networking) 0 Answers
Multiple Cars not working 1 Answer