Camera problem when client joins

Hi,

I was trying to code a simple multiplayer for my game. I got stuck when setting the correct camera for a joined player. When the client player joins my host player the cameras get switched. Camera A for player A is now snapped to player B and vice versa. Here is my code for the player after the prefab:
Code

using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;

public class PlayerSetup : NetworkBehaviour {

    int counter;
    Camera sceneCamera;
    private GUIStyle winStyle = new GUIStyle();
    public Camera cameraUp;
    public Camera cameraDown;
    void Start ()
    {
        if (!isLocalPlayer)
        {
            this.GetComponent<SimpleCarController>().enabled = false;
            return;
        }
        else
        {
            Camera.main.enabled = false;
            cameraUp.enabled = true;
            Debug.Log("Camera switch");
            winStyle.fontSize = 80;
            winStyle.alignment = TextAnchor.UpperCenter;
            winStyle.normal.textColor = Color.white;

            RegisterPlayer();
            OnGUI();
        }

     }
   
    void RegisterPlayer()
    {
        string _ID = "Player " + GetComponent<NetworkIdentity>().netId;
        cameraUp.name = "camUp" + _ID;
        cameraDown.name = "camDown" + _ID;
        transform.name = _ID;
        Debug.Log("Register was here");
    }

    void FixUpdate()
    {
        OnGUI();
    }

    public void OnGUI()
    {
        counter = count.currentCounthub;
        GUI.Label(new Rect(20f, 20f, 100f, 30f), "Count: " + counter.ToString());

        //win-lose
        if (counter == 3)
        {
            GUI.Label(new Rect(Screen.width / 2, Screen.height / 2, 0f, 30f), "You Win!", winStyle);
        }

    }
}

Can you please help me to figure out what went wrong?

To be honest I’m quite confused what is happening but anyway if you are using Unet try to use multiplayer methods like OnStartClient/OnStartLocalPlayer and of course Commands and ClientRpc,

From what I understood and what I would try to do is:

  • SimpleCarController disable it on playerPrefab and make it enabled OnStartLocalPlayer (within the simpleCarController or whatever class it manages it)
  • Enable camera in OnStartLocalPlayer as well and make the RegisterPlayer a [Command]

PS I think you don’t have to call OnGUI method anywhere (espiecally on any update event) because it will be cast on it’s own so remove it’s call on fixedUpdate. If my advices won’t work please explain what are you trying to accomplish so I can understand it please :slight_smile: Cheers and good luck.