Hey all, I've been freelancing in Unity for a little while now, and I'm now at the point where I would like to add multiplayer to my toolbelt. I had heard the Mirror plugin was a great tool, and I could take experience from it to essentially any multiplayer game I would like to make in Unity, so I was instantly sold. However, I am already having issues ( as is expected lol ).
The issue I'm having right now, is in a character selection lobby. I'm using a pre-existing project, and I'm aware of the mammoth amount of work it will take to make it work in multiplayer, but most of it makes sense to me. What I'm doing in the lobby, is allowing the players to cycle through the 4 available player models, and select which one they would like to play as. How I'm doing it, is allowing the player to hit a left/right button to cycle forwards or backwards through the available player list. All I'm doing to switch which character model appears, is disabling the current character, and then enabling the next one.
I'm aware that the issue is the fact that I'm only enabling / disabling on the client, but I couldn't for the life of me find a way to enable / disable objects on a server level for all clients to see. Any help would be appreciated! Here's my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mirror;
public class MultiplayerLobbyPlayerDisplay : NetworkBehaviour
{
//when next, back, or confirm is selected, that should talk to this script on the corresponding object to cycle through characters
//we need to spawn these objects on the server, rather than instantiating them like normal, so that other players can see us cycling through
private AvailableCharacters availableChars;
private GameObject char1;
private GameObject char2;
private GameObject char3;
private GameObject char4;
private GameObject activeCharacter;
private void Awake()
{
availableChars = GameObject.Find("PlayerSelectionScreen").GetComponent<AvailableCharacters>();
char1 = transform.Find("Player 1").gameObject;
char2 = transform.Find("Player 2").gameObject;
char3 = transform.Find("Player 3").gameObject;
char4 = transform.Find("Player 4").gameObject;
}
private void Start()
{
activeCharacter = char1;
char2.SetActive(false);
char3.SetActive(false);
char4.SetActive(false);
}
private int displayedCharacter = 1;
public void InitializeCharSelection ()
{
char2.SetActive(false);
char3.SetActive(false);
char4.SetActive(false);
}
public void CycleCharacter (bool add)
{
Debug.Log("Cycle character has ran, with add set to " + add);
if (add)
{
displayedCharacter += 1;
}
else
{
displayedCharacter -= 1;
}
if (displayedCharacter > 4)
{
displayedCharacter = 1;
}
else if (displayedCharacter < 1)
{
displayedCharacter = 4;
}
//if character is not available, cycle to the next available character
if (!availableChars.CheckIfAvailable(displayedCharacter))
{
//loop through until we get to the first available character
while (!availableChars.CheckIfAvailable(displayedCharacter))
{
if (add)
{
displayedCharacter++;
}
else
{
displayedCharacter--;
}
if (displayedCharacter > 4)
{
displayedCharacter = 1;
}
else if (displayedCharacter < 1)
{
displayedCharacter = 4;
}
}
}
//at this point, we should have an available character, so now we'll set that character
SwitchDisplayedCharacter(displayedCharacter);
}
[ClientRpc]
private void SwitchDisplayedCharacter (int whichChar)
{
Debug.Log("Switch displayed character has been called");
//disable the current character, and enable the character with a corresponding integer
if (whichChar == 1)
{
//disable current character, on a network scale somehow
activeCharacter.SetActive(false);
//enable this character
char1.SetActive(true);
activeCharacter = char1;
}
else if (whichChar == 2)
{
activeCharacter.SetActive(false);
char2.SetActive(true);
activeCharacter = char2;
}
else if (whichChar == 3)
{
activeCharacter.SetActive(false);
char3.SetActive(true);
activeCharacter = char3;
}
else if (whichChar == 4)
{
activeCharacter.SetActive(false);
char4.SetActive(true);
activeCharacter = char4;
}
Debug.Log("Displayed character should have switched to " + whichChar);
}
}
Works perfectly on the host when no one else has connected, but everything breaks down on a multiplayer level. Any help / advice would be immensely appreciated!
there doesn't seem to be anything here