- Home /
This Code Does Not Spawn A Player Why?
using UnityEngine; using System.Collections;
public class NetworkManager : MonoBehaviour {
public Camera standbyCamera;
SpawnSpot[] spawnSpots;
// Use this for initialization
void Start () {
spawnSpots = GameObject.FindObjectsOfType<SpawnSpot>();
Connect ();
}
// Update is called once per frame
void Connect () {
PhotonNetwork.ConnectUsingSettings ("V1");
}
void OnGUI () {
GUILayout.Label (PhotonNetwork.connectionStateDetailed.ToString ());
}
void OnJoinedLobby (){
PhotonNetwork.JoinRandomRoom ();
}
void OnPhotonRandomJoinFailed (){
PhotonNetwork.CreateRoom (null);
}
void OnJoinedRoom (){
SpawnMyPlayer ();
}
void SpawnMyPlayer (){
if (spawnSpots == null) {
return;
}
SpawnSpot mySpawnSpot = spawnSpots[Random.Range(0,spawnSpots.Length)];
PhotonNetwork.Instantiate ("PlayerController",mySpawnSpot.transform.position,mySpawnSpot.transform.rotation,1);
standbyCamera.enabled = false;
}
//mySpawnSpot.transform.position,mySpawnSpot.transform.rotation
}
right before you instantiate, put Debug.Log (spawnSpots.Length); What does it return?
I fixed this problem but I have a new problem over here
http://answers.unity3d.com/questions/941020/nullrefenceexception-on-object-please-help.html
If you could help me there have been looking for an answer for hours.
Answer by WazzaM · Apr 06, 2015 at 02:20 PM
Hi there
1) I tried to follow the link you gave but got Access Denied by the forum (?) so I'm answering here.
2) I think the problem with the Instantiate(..) call is the issue. What will be instantiated? see below...
Normally with Instantiate() calls you need either an existing GameObject to clone or a Prefab but a string (object name?) is almost never supported. It's possible you don't get a compile error because an overload is expecting to get an hierarchy path to an object to clone.
A simpler and often better way is to make a public GameObject field in your class and then set that to the prefab or Scene object using the Inspector Window.
If you are now getting a NullReferenceException this could explain it... but I make no guarantees - I haven't tried the Photon API.
Your answer
Follow this Question
Related Questions
player spawning 0 Answers
player Spawning Error on Photon (PhotonNetwork.Instantiate) 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Moving player along path? 1 Answer