how to spawn players in same room? they cannot find each other..

Options


Hi all, i have a problem in using photon. I want to spawn players in the same room but each player cannot find each other. There is my situation in below.


scene 1 : connet to photon server (I tested in unity engine & build engine)

  • unity engine(player1) : connect to photon & join the room
  • build engine(player2) : connect to photon & join the room
  • But I guess they create the their own room.


using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Photon.Pun;
using Photon.Realtime;
using TMPro;


public class NetworkManager : MonoBehaviourPunCallbacks
{
    private string gameVersion = "1";
    public TextMeshProUGUI connectionInfoText;
    public Button joinButton;
    public Button optionButton;
    public Button exitButton;

    private void Start()
    {
        PhotonNetwork.GameVersion = gameVersion;
        PhotonNetwork.ConnectUsingSettings();
        connectionInfoText.text = "connect to master server";
    }


    public override void OnConnectedToMaster()
    {
        connectionInfoText.text = "online : success";
    }


    public override void OnDisconnected(DisconnectCause cause)
    {
        connectionInfoText.text = "offline : failed \n try again";
        PhotonNetwork.ConnectUsingSettings();
    }


    public void Connect()
    {
        if (PhotonNetwork.IsConnected)
        {
            connectionInfoText.text = "connet to the room";
            PhotonNetwork.JoinRandomRoom();
        }


        else
        {
            connectionInfoText.text = "offline : failed \n try again";
            PhotonNetwork.ConnectUsingSettings();
        }
    }


    public override void OnJoinRandomFailed(short returnCode, string message)
    {
        connectionInfoText.text = "there is no empty. Create new room";
        PhotonNetwork.CreateRoom("Room No.1", new RoomOptions { MaxPlayers = 4 });
    }


    public override void OnJoinedRoom()
    {
        connectionInfoText.text = "you are in room";
    }
}


scene 2 : player customiztion

  • custom separtely and DontDestroy the "Player"(which is customed) gameObject
  • "Player" gameObject : has Photon View & Transform View & Rigidbody View
  • there is "Player" script in below.


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using Photon.Realtime;


public class Player : MonoBehaviourPunCallbacks
{
    GameObject player;


    private void Start()
    {
        player = this.gameObject;
        DontDestroyOnLoad(player);
    }
}


scene 3 : spawn players on each spawnPositions

  • there is "PlayerSpawner" script (just translate "Player" gameObject to each spawnPosition)
  • In Scene 3, there is just one "Player" gameObject in hierachy. (just their own)


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using Photon.Realtime;


public class PlayerSpawner : MonoBehaviourPunCallbacks
{
    public List<Transform> positionList = new List<Transform>();
    public int index;


    public void Start()
    {
        GameObject player = GameObject.FindGameObjectWithTag("Player");
        Transform[] positions = GameObject.Find("SpawnPosition").GetComponentsInChildren<Transform>();
        foreach (Transform pos in positions)
            positionList.Add(pos);

        index = Random.Range(0, positionList.Count);
        player.transform.position = positionList[index].position;
        positionList.RemoveAt(index);
    }
}


This is the final try, I've been trying to solve this problem for a week..

First time, I tried making "Player" gameObject to prefab in Resources Folder. But in build engine, it didn't run. There is a script about it("SavePrefab" script) in below.


#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine;
using UnityEngine.UI;
using System.Collections;


public class SavePrefab : MonoBehaviour
{
    public GameObject saveObject;
    public Text savePathText;
    public void Save()
    {
#if UNITY_EDITOR
        if (savePathText == null || saveObject == null || string.IsNullOrEmpty(savePathText.text))
            return;
        // The paths to the mesh/prefab assets.
        string prefabPath = "Assets/Resources/Player" + ".prefab";


        // Delete the assets if they already exist.
        AssetDatabase.DeleteAsset(prefabPath);


        // Save the transform's GameObject as a prefab asset.
        PrefabUtility.SaveAsPrefabAsset(saveObject, prefabPath);
#endif
    }
}



I want to solve this problem and I need your help.

Answers

  • peewoong
    peewoong
    edited December 2021
    Options

    I checked matchmaking checklist already. I debugged the PhotonNetwork.CloudRegion and it said "Null". But i can't guess why is it happening.

    Could anybody help me? :(



    private readonly string gameVersion = "2.40";
        private string userId = "peewoong";
        public TextMeshProUGUI connectionInfoText;
    
    
        private void Awake()
        {
            PhotonNetwork.GameVersion = gameVersion;
            PhotonNetwork.ConnectUsingSettings();
        }
    
    
        private void Start()
        {
            connectionInfoText.text = "포톤 매니저 시작";
            PhotonNetwork.NickName = userId;
            Debug.Log(PhotonNetwork.CloudRegion);
        }
    
    
        public override void OnConnectedToMaster()
        {
            connectionInfoText.text = "포톤 서버에 접속";
            PhotonNetwork.JoinRandomRoom();
        }
    
    
        public override void OnJoinRandomFailed(short returnCode, string message)
        {
            connectionInfoText.text = "랜덤 룸 접속 실패";
            RoomOptions ro = new RoomOptions();
            ro.IsOpen = true;
            ro.IsVisible = true;
            ro.MaxPlayers = 4;
    
    
            PhotonNetwork.CreateRoom("room 1", ro);
        }
    
    
        public override void OnCreatedRoom()
        {
            connectionInfoText.text = "방 생성 완료";
        }
    
    
        public override void OnJoinedRoom()
        {
            connectionInfoText.text = "방 입장 완료";
        }
    


    Some Korean word, you don't need to understand them..

  • peewoong
    Options

    I checked the Matchmaking CheckList already, and debugged the region, but it said "Null". I don't know why is it happening :(

    Is there any problem with my script? I want to solve this problem asap, please.



    private readonly string gameVersion = "2.40";
        private string userId = "peewoong";
        public TextMeshProUGUI connectionInfoText;
    
    
        private void Awake()
        {
            PhotonNetwork.GameVersion = gameVersion;
            PhotonNetwork.ConnectUsingSettings();
        }
    
    
        private void Start()
        {
            connectionInfoText.text = "포톤 매니저 시작";
            PhotonNetwork.NickName = userId;
            Debug.Log(PhotonNetwork.CloudRegion);
        }
    
    
        public override void OnConnectedToMaster()
        {
            connectionInfoText.text = "포톤 서버에 접속";
            PhotonNetwork.JoinRandomRoom();
        }
    
    
        public override void OnJoinRandomFailed(short returnCode, string message)
        {
            connectionInfoText.text = "랜덤 룸 접속 실패";
            RoomOptions ro = new RoomOptions();
            ro.IsOpen = true;
            ro.IsVisible = true;
            ro.MaxPlayers = 4;
    
    
            PhotonNetwork.CreateRoom("room 1", ro);
        }
    
    
        public override void OnCreatedRoom()
        {
            connectionInfoText.text = "방 생성 완료";
        }
    
    
        public override void OnJoinedRoom()
        {
            connectionInfoText.text = "방 입장 완료";
        }
    
  • peewoong
    Options

    i quit