- Home /
NetworkManager spawns two of same prefab
I have a 2D multiplayer game with a character selection screen and script that carries the selection to the next screen where both players spawn. The only problem is that each client sees both themselves and the other player as the same model, rather than different models depending on which one each player picked.
The prefab I use in the NetworkManager is called CharacterList and has my player game object prefabs as children. Players see themselves as the correct character but the opponent is always the identical character model. For example, if I have a red character and a blue character, both characters on one screen appear blue, while both characters on the other screen appear red.
Here's my character selection script which is attached to CharacterList:
public class CharacterSelection : MonoBehaviour {
private GameObject[] characterList;
private int index;
private void Start () {
index = PlayerPrefs.GetInt ("CharacterSelected");
characterList = new GameObject[transform.childCount];
for (int i = 0; i < transform.childCount; i++)
characterList [i] = transform.GetChild(i).gameObject;
foreach(GameObject go in characterList)
{
go.SetActive (false);
if (characterList [index])
characterList [index].SetActive (true);
}
}
public void ToggleLeft()
{
characterList [index].SetActive (false);
index--;
if (index < 0)
index = characterList.Length - 1;
characterList [index].SetActive (true);
}
public void ToggleRight()
{
characterList [index].SetActive (false);
index++;
if (index == characterList.Length)
index = 0;
characterList [index].SetActive (true);
}
public void ConfirmButton()
{
PlayerPrefs.SetInt ("CharacterSelected", index);
SceneManager.LoadScene ("MultiplayerBranch");
}
}
Answer by tormentoarmagedoom · Mar 26, 2018 at 03:49 PM
Good day.
If you are creating a multiplayer game, you first need to know how it works. You are asking a very simple question, but the answer is not simple if you dont understand whats a client, a server, a host, and how they execute and share all data.
You should look for this pages in Unity Api documentation:
isServer
isClient
isLocalPlayer
ClientRpcAttribute
CommandAttribute
With this 5 tools, you may decide what will be painted, where, by who and who can see it... you need to know how they work and what they do perfectly if rpetend to do amultiplayer game.
Bye! :D
Your answer
Follow this Question
Related Questions
Check Standalone device network connection 0 Answers
How to make private room in Photon PUN 2? 0 Answers
Multiplayer - simple example with no collider 0 Answers
Multiplayer advanced - Single Player in Multiplayer game, using another Hosting services. 0 Answers
Remove Player and Close Connection in Multiplayer Game 0 Answers