- Home /
Photon - Loads a level but doesn't spawn player.
I added a command to my game to switch levels (/level 0-2). It loads the level but it gets stuck on joining and the player doesn't spawn for some reason, and I don't know why. It should be re-instantiating the player OnJoinedRoom(). Here is my code.
Chat.cs
[RPC]
void ApplyGlobalChatText(string name, string msg)
{
ChatEntry entry = new ChatEntry();
entry.name = name;
entry.text = msg;
if (entry.text.Contains("/level 0"))
{
PhotonNetwork.isMessageQueueRunning = true;
PhotonNetwork.LoadLevel(0);
}
if (entry.text.Contains("/level 1"))
{
PhotonNetwork.isMessageQueueRunning = true;
PhotonNetwork.LoadLevel(1);
}
if (entry.text.Contains("/level 2"))
{
PhotonNetwork.isMessageQueueRunning = true;
PhotonNetwork.LoadLevel(2);
}
chatEntries.Add(entry);
if (chatEntries.Count > 4)
{
chatEntries.RemoveAt(0);
}
scrollPosition.y = 1000000;
//555 This was erasing chat for everyone
//inputField = "";
}
MPManger.cs
using UnityEngine;
using System.Collections;
public class MPManager : Photon.MonoBehaviour
{
public GameObject[] spawnPoints;
// Use this for initialization
void Start()
{
spawnPoints = GameObject.FindGameObjectsWithTag("SpawnPoint");
PhotonNetwork.ConnectUsingSettings("0.4");
//int spawnRand = Random.Range(0, spawnPoints.Length);
}
void OnGUI()
{
GUILayout.Label(PhotonNetwork.connectionStateDetailed.ToString());
}
void OnJoinedLobby()
{
PhotonNetwork.JoinRandomRoom();
}
void OnPhotonRandomJoinFailed()
{
PhotonNetwork.CreateRoom("Aftermath - AW");
}
void OnJoinedRoom()
{
//555 Stupid inconsistency with random.range, it starts at 1 instead of 0.
GameObject myPlayer = PhotonNetwork.Instantiate("Player_MP", spawnPoints[Random.Range(1, spawnPoints.Length)].transform.position, spawnPoints[Random.Range(1, spawnPoints.Length)].transform.rotation, 0);
string playerNumber = Random.Range(1, 1000).ToString();
myPlayer.GetComponent<PhotonView>().owner.name = playerNumber;
myPlayer.name = "Player" + playerNumber;
}
void OnDisconnectedFromPhoton()
{
//PhotonNetwork.DestroyPlayerObjects(photonView.ownerId);
//print(photonView.ownerId + " objects destroyed from server");
}
}
What am I doing wrong? The spawnpoints are in each level also. Actually I just thought of something. Is start for the spawnpoints getting called when I switch levels? Maybe that is actually the problem. But I'm not getting a null ref. The mpmanger is on a gameobject prefab that is in every level.
Your code has 2, onjoinedroom, one, making you connect to a random room?, and two creating the player. Use only the player one.
I did it the way photon shows to do it, if I disable the random room the first room never gets created. Also can you be more specific? there is OnPhotonRandomJoinFailed(),OnJoinedRoom(), OnJoinedLobby() .
Photon works by joining a lobby first, then a client creates a room(game) so its before room.
Answer by Shkarface-Noori · Jan 10, 2014 at 07:14 AM
Photon uses prefab cache to cache prefabs for faster performance...you have to cache your prefab in the resources folder with the exact name used in PhotonNetwork.Instantiate
Yeah that is what this line is doing. Loading from my resources folder, but It doesn't re-spawn, reinstantiate (give a person the prefab to control) the player when I do a level change. I know this line is working because Im able to join a room, with a player. But the problem is when I use my switch level command it doesn't respawn the player into the new loaded level.
GameObject myPlayer = PhotonNetwork.Instantiate("Player_$$anonymous$$P", spawnPoints[Random.Range(1, spawnPoints.Length)].transform.position, spawnPoints[Random.Range(1, spawnPoints.Length)].transform.rotation, 0);
Your answer
Follow this Question
Related Questions
Spawn players in unique places (Photon networking) Object sync problem for entering players. 0 Answers
Unity networking tutorial? 6 Answers
MLAPI spawn player prefab 2 Answers
Photon Unity Network - Refresh/Tickrate 2 Answers
Unity Photon syncing objects 0 Answers