1

I'm trying to develop a 3D multiplayer game with Unity. I don't know much about Photon, there are similar questions to the one I'm going to ask, but I still haven't found a solution. I will be glad if you help. I have two scenes named "menu" and "game". In the menu scene, users make character selection after authenticating with playfab. After completing the selection, they connect to the lobby and set up a room and load the game scene. So far everything is successful. However, when the game scene is loaded, I have difficulty loading the characters selected by the users into the scene.

Here is the code file where I make the users choose their characters:

public class choose : MonoBehaviour {

private GameObject[] characterList;
private int index;
PhotonView PV;

private void Awake()
{
    PV = GetComponent<PhotonView>();
}

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 kaydetbuton() {
    PlayerPrefs.SetInt ("CharacterSelected", index);
    
    
} }

Here is the code file where I make the characters in the game move:

public class control : MonoBehaviour {

public FixedJoystick LeftJoystick;
private GameObject leftjoystick;
public FixedButton Button;
private GameObject button;
public FixedTouchField TouchField;
private GameObject touchField;
protected ThirdPersonUserControl Control;

protected float CameraAngle;
protected float CameraAngleSpeed = 0.2f;

PhotonView PV;

void Awake()
{

    PV = GetComponent<PhotonView>();
}
void Start()
{
    if (!PV.IsMine)
        return;
    Control = GetComponent<ThirdPersonUserControl>();

    leftjoystick = GameObject.Find("Fixed Joystick");
    if (leftjoystick != null)
    {
        LeftJoystick = leftjoystick.GetComponent<FixedJoystick>();
    }
    button = GameObject.Find("Handle (1)");
    if (button != null)
    {
        Button = button.GetComponent<FixedButton>();
    }
    touchField = GameObject.Find("tfield");
    if (touchField != null)
    {
        TouchField = touchField.GetComponent<FixedTouchField>();
    }
}
  void FixedUpdate() {

    if (PV.IsMine)
    {
        Control.m_Jump = Button.Pressed;
        Control.Hinput = LeftJoystick.Direction.x;
        Control.Vinput = LeftJoystick.Direction.y;

        CameraAngle += TouchField.TouchDist.x * CameraAngleSpeed;

        Camera.main.transform.position = transform.position + Quaternion.AngleAxis(CameraAngle, Vector3.up) * new Vector3(1, 2, 3);
        Camera.main.transform.rotation = Quaternion.LookRotation(transform.position + Vector3.up * 2f - Camera.main.transform.position, Vector3.up);
    }
} }

There is a game object named "karakteryükle" in the menu scene. The code file named "choose" is in this object. There are 4 characters in this game object. Each character has a code file named "control", photon view, photon transform view, animator view component. And the game object named "karakteryükle" is also available as a prefab in the Resources folder.

I am sharing the picture of the components loaded on each character

I shared a picture of the game object named "karakter yükle"

I'm trying to load "karakter yükle" when the scene named game is loaded

PhotonNetwork.Instantiate("karakteryükle", new Vector3((float)-0.43, (float)1.1, (float)-25.84), Quaternion.identity, 0, null);

Result: The "karakteryükle" is loaded onto the stage, but the same character is loaded for each player, the character chosen by each player is not loaded. I need your opinion on this.

druu55
  • 35
  • 6
  • We need to see how you spawn the characters. Is there any particular reason you included the movement code? – hijinxbassist Mar 17 '22 at 20:33
  • I downloaded the characters from mixamo. I created these characters by modeling the character named "ethan" in the Unity Standard Assets. So the new characters are using etan animations, I just use the ones I downloaded from mixamo as physical images. I'm updating my question and adding a new screenshot to make it clearer. Also, there's no particular reason why I added the movement code. – druu55 Mar 17 '22 at 21:42
  • How do you spawn the players character in code. Please show this portion of code where you use the selected character index to spawn the appropriate character for the player. For instance, there is `PlayerPrefs.SetInt ("CharacterSelected", index);` in your menu code, where do you use `PlayerPrefs.GetInt("CharacterSelected")` in the game scene? – hijinxbassist Mar 17 '22 at 22:05
  • I just load the "karakteryükle" game object in the game scene. The code file named "choose", where I let the users choose their characters, is in the "karakteryükle". The characters are also in the "karakteryükle". I load the "karakteryükle" into the game scene and the selected character is spawn in this way. – druu55 Mar 17 '22 at 22:28

1 Answers1

0

Each player only knows their own setting for the index, because they use the value set in PlayerPrefs.

private void Start()
{   
    index = PlayerPrefs.GetInt("CharacterSelected");
}

This works for our local player, no problem there. But what happens when a different player enters the scene.

  1. The playerObject is spawned on each client.
  2. Each client handles that playerObject locally (this is the reason IsMine exist).
  3. Player1 executes index = PlayerPrefs.GetInt(..) on their copy of Player2.

What you can do is send a buffered RPC to set the selected character on those remote copies. We want to buffer the rpc so new players change their remote copies of everyone to the appropriate character.

myPhotonView.RPC("SetCharacterIndex", RpcTarget.OthersBuffered, index);

and the corresponding RPC method

[PunRPC]
private void SetCharacterIndex(int index)
{
    // Disable other characters and enable the one at this index
}

In the end you end up with something like

void Start()
{
    characterList = new GameObject[transform.childCount];

    for (int i=0; i< transform.childCount; i++) 
    {
        characterList[i] = transform.GetChild(i).gameObject;
        characterList[i].SetActive(false);
    }

    if (isMine)
    {
        index = PlayerPrefs.GetInt("CharacterSelected");
        
        // Notify all remote copies of us to change their index
        //
        photonView.RPC("SetCharacterIndex", RpcTarget.OthersBuffered, index);

        // Set the index locally
        //
        SetCharacterIndex(index);
    }
}
[PunRPC]
private void SetCharacterIndex(int index)
{
    if (characterList [index])
        characterList [index].SetActive (true);
}

Hopefully that helps clear up the reason this happens (networking can be confusing at times).

hijinxbassist
  • 3,667
  • 1
  • 18
  • 23
  • Player 1 is loading the scene successfully, player 2 is loading as well, but the two cannot see each other. There are 2 "karakteryükle" clones in the Unity hierarchy, but all of the 2nd player's characters seem to be turned off. If I make any character true manually then the 2nd player is now loaded on the 1st player's screen as well. I am getting the following error in the editor. `NullReferenceException: Object reference not set to an instance of an object choose.SetCharacterIndex (System.Int32 index) (at Assets/scripts/choose.cs:45)` Line 45 has this: `if (characterList[index])` – druu55 Mar 18 '22 at 17:13
  • The null is most likely from the characterList not being initialized in time. Try moving the characterList initialization code into Awake. – hijinxbassist Mar 18 '22 at 18:24
  • I really thank you for your interest. Everything is working properly – druu55 Mar 18 '22 at 18:45
  • happy to help, best of luck! – hijinxbassist Mar 19 '22 at 18:35