Hello guys, I try to summarize my problem
.
First I have a GameManager prefab with a script to manage multiplayer part of the game, and here I made the code what is Instantiate my player. This prefab hasn't PhotonView component and I Instantiate my player like this.
Code (CSharp):
GameObject myPlayer = (GameObject)PhotonNetwork.Instantiate("aTestPrefab", spawnPoints[0].transform.position, Quaternion.identity, 0);
It's working, well and the game run with no error if I run the build client and the editor same time and test it and its fine. My problem starts there when I want to make a spawn system to not spawning two players on the same spawn.
Code (CSharp):
private void Start()
{
if (PhotonNetwork.isMasterClient)
{
UploadFreeSpawnPoints();
SpawnPlayer();
}
}
Code (CSharp):
public override void OnPhotonPlayerConnected(PhotonPlayer other)
{
if (!PhotonNetwork.isMasterClient)
{
Debug.Log("OnPhotonPlayerConnected isMasterClient " + PhotonNetwork.isMasterClient); // called before OnPhotonPlayerDisconnected
SpawnPlayer();
}
}
Code (CSharp):
private void SpawnPlayer()
{
///Increase connected player numbers
#pragma warning disable CS0618 // Type or member is obsolete
int connectedPlayers = (int)PhotonNetwork.room.customProperties["ConnectedPlayers"];
#pragma warning restore CS0618 // Type or member is obsolete
Debug.Log(connectedPlayers);
connectedPlayers++;
ExitGames.Client.Photon.Hashtable hashtable = new ExitGames.Client.Photon.Hashtable
{
{ "ConnectedPlayers", connectedPlayers }
};
PhotonNetwork.room.SetCustomProperties(hashtable);
// we're in a room. spawn a character for the local player. it gets synced by using PhotonNetwork.Instantiate.
SpawnPoint random = ReservePlayerSpawnPoint();
if (random == null)
Debug.LogError("Spawn point is nullPointer(GameManager)");
else
Debug.Log(random);
GameObject myPlayer = (GameObject)PhotonNetwork.Instantiate("aTestPrefab", random.transform.position, Quaternion.identity, 0);
}
private void UploadFreeSpawnPoints()
{
for (int i = 0; i < spawnPoints.Length; i++)
{
freeSpawn[i] = Int16.Parse(spawnPoints[i].gameObject.name.ToString());
}
ExitGames.Client.Photon.Hashtable freeSpawnHash = new ExitGames.Client.Photon.Hashtable
{
{ "FreeSpawnPoints", freeSpawn }
};
PhotonNetwork.room.SetCustomProperties(freeSpawnHash);
}
private SpawnPoint ReservePlayerSpawnPoint()
{
#pragma warning disable CS0618 // Type or member is obsolete
freeSpawn = (int[])PhotonNetwork.room.customProperties["FreeSpawnPoints"];
#pragma warning restore CS0618 // Type or member is obsolete
Debug.Log(freeSpawn);
int spawnPoint = freeSpawn[UnityEngine.Random.Range(0, freeSpawn.Length)];
SpawnPoint returnPoint = null;
foreach (SpawnPoint spawn in spawnPoints)
{
if (spawn.gameObject.name == spawnPoint.ToString())
returnPoint = spawn;
}
return returnPoint;
}
Here is the code, if I not use the Properties part I don't get an error, if I use the game is running but sync all objects and its full bugged. The main question is that, how can the properties effect the game
.

First I have a GameManager prefab with a script to manage multiplayer part of the game, and here I made the code what is Instantiate my player. This prefab hasn't PhotonView component and I Instantiate my player like this.
Code (CSharp):
GameObject myPlayer = (GameObject)PhotonNetwork.Instantiate("aTestPrefab", spawnPoints[0].transform.position, Quaternion.identity, 0);
It's working, well and the game run with no error if I run the build client and the editor same time and test it and its fine. My problem starts there when I want to make a spawn system to not spawning two players on the same spawn.
Code (CSharp):
private void Start()
{
if (PhotonNetwork.isMasterClient)
{
UploadFreeSpawnPoints();
SpawnPlayer();
}
}
Code (CSharp):
public override void OnPhotonPlayerConnected(PhotonPlayer other)
{
if (!PhotonNetwork.isMasterClient)
{
Debug.Log("OnPhotonPlayerConnected isMasterClient " + PhotonNetwork.isMasterClient); // called before OnPhotonPlayerDisconnected
SpawnPlayer();
}
}
Code (CSharp):
private void SpawnPlayer()
{
///Increase connected player numbers
#pragma warning disable CS0618 // Type or member is obsolete
int connectedPlayers = (int)PhotonNetwork.room.customProperties["ConnectedPlayers"];
#pragma warning restore CS0618 // Type or member is obsolete
Debug.Log(connectedPlayers);
connectedPlayers++;
ExitGames.Client.Photon.Hashtable hashtable = new ExitGames.Client.Photon.Hashtable
{
{ "ConnectedPlayers", connectedPlayers }
};
PhotonNetwork.room.SetCustomProperties(hashtable);
// we're in a room. spawn a character for the local player. it gets synced by using PhotonNetwork.Instantiate.
SpawnPoint random = ReservePlayerSpawnPoint();
if (random == null)
Debug.LogError("Spawn point is nullPointer(GameManager)");
else
Debug.Log(random);
GameObject myPlayer = (GameObject)PhotonNetwork.Instantiate("aTestPrefab", random.transform.position, Quaternion.identity, 0);
}
private void UploadFreeSpawnPoints()
{
for (int i = 0; i < spawnPoints.Length; i++)
{
freeSpawn[i] = Int16.Parse(spawnPoints[i].gameObject.name.ToString());
}
ExitGames.Client.Photon.Hashtable freeSpawnHash = new ExitGames.Client.Photon.Hashtable
{
{ "FreeSpawnPoints", freeSpawn }
};
PhotonNetwork.room.SetCustomProperties(freeSpawnHash);
}
private SpawnPoint ReservePlayerSpawnPoint()
{
#pragma warning disable CS0618 // Type or member is obsolete
freeSpawn = (int[])PhotonNetwork.room.customProperties["FreeSpawnPoints"];
#pragma warning restore CS0618 // Type or member is obsolete
Debug.Log(freeSpawn);
int spawnPoint = freeSpawn[UnityEngine.Random.Range(0, freeSpawn.Length)];
SpawnPoint returnPoint = null;
foreach (SpawnPoint spawn in spawnPoints)
{
if (spawn.gameObject.name == spawnPoint.ToString())
returnPoint = spawn;
}
return returnPoint;
}
Here is the code, if I not use the Properties part I don't get an error, if I use the game is running but sync all objects and its full bugged. The main question is that, how can the properties effect the game
