hey! I’ve just written up a script which SHOULD allow for players to spawn at a random spawn point once starting the game… BUT I have an error :-
IndexOutOfRangeException: Array index is out of range.
NetworkManager.SpawnMyPlayer () (at
Assets/NetworkManager.cs:45)
NetworkManager.OnJoinedRoom () (at
Assets/NetworkManager.cs:36)
UnityEngine.GameObject:SendMessage(String,
Object, SendMessageOptions)
NetworkingPeer:SendMonoMessage(PhotonNetworkingMessage,
Object) (at Assets/_Imported/Photon
Unity
Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:1769)
NetworkingPeer:OnEvent(EventData) (at
Assets/_Imported/Photon Unity
Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:1612)
ExitGames.Client.Photon.PeerBase:DeserializeMessageAndCallback(Byte)
ExitGames.Client.Photon.EnetPeer:DispatchIncomingCommands()
ExitGames.Client.Photon.PhotonPeer:DispatchIncomingCommands()
PhotonHandler:Update() (at
Assets/_Imported/Photon Unity
Networking/Plugins/PhotonNetwork/PhotonHandler.cs:76)
Here is the script -
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 ();
}
void Connect() {
PhotonNetwork.ConnectUsingSettings( "FPS Game 0.00002" );
}
void OnGUI() {
GUILayout.Label( PhotonNetwork.connectionStateDetailed.ToString() );
}
void OnJoinedLobby() {
Debug.Log ("OnJoinedLobby");
PhotonNetwork.JoinRandomRoom();
}
void OnPhotonRandomJoinFailed() {
Debug.Log ("OnPhotonRandomJoinFailed");
PhotonNetwork.CreateRoom( null );
}
void OnJoinedRoom() {
Debug.Log ("OnJoinedRoom");
SpawnMyPlayer();
}
void SpawnMyPlayer() {
if(spawnSpots == null) {
Debug.LogError ("NO SPAWN POINTS FOUND!!!");
return;
}
SpawnSpot mySpawnSpot = spawnSpots[ Random.Range (0, spawnSpots.Length) ];
GameObject myPlayerGO = (GameObject)PhotonNetwork.Instantiate("Player", mySpawnSpot.transform.position, mySpawnSpot.transform.rotation, 0);
standbyCamera.enabled = false;
((MonoBehaviour)myPlayerGO.GetComponent("FPSInputController")).enabled = true;
((MonoBehaviour)myPlayerGO.GetComponent("MouseLook")).enabled = true;
myPlayerGO.transform.FindChild("Main Camera").gameObject.SetActive(true);
}
}
I have placed my “SpawnSpot”'s in the scene although it still won’t work. I have also put their prefab in the resources folder so I’m not sure what’s wrong…
If you need any more information please feel free to ask! ![]()
Try printing spawnSpots.Length before getting the mySpawnPoint Also try obtaining the random address and saving it in an int then print it before that too See what do you get Does it work if you put a static value (like 0) ?
– JiMMaR