Unity array out of range exception, using photon unity network.

using UnityEngine;
using System.Collections;

public class networkmanager : MonoBehaviour {

	public Camera lobbycamera;
	SpawnSpot[] spawnSpots;

	void Start () 
	{
		spawnSpots = GameObject.FindObjectsOfType<SpawnSpot> ();

		Connect ();
	}

	void Connect() 
	{
		PhotonNetwork.ConnectUsingSettings ("0.0.1");
	}

	void OnGUI() 
	{
		GUILayout.Label (PhotonNetwork.connectionStateDetailed.ToString());
	}

	void OnJoinedLobby() 
	{
		PhotonNetwork.JoinRandomRoom ();
	}

	void OnPhotonRandomJoinFailed() 
	{
		PhotonNetwork.CreateRoom (null);
	}

	void OnJoinedRoom()
	{
		SpawnMyPlayer ();
	}

	void SpawnMyPlayer ()
	{
		SpawnSpot mySpawnSpot = spawnSpots[ Random.Range (0, spawnSpots.Length) ];
		GameObject myPlayerGO = (GameObject)PhotonNetwork.Instantiate("PlayerController", mySpawnSpot.transform.position, mySpawnSpot.transform.rotation, 0);
		lobbycamera.enabled = false;
		
		((MonoBehaviour)myPlayerGO.GetComponent("FPSInputController")).enabled = true;
		((MonoBehaviour)myPlayerGO.GetComponent("MouseLook")).enabled = true;
		myPlayerGO.transform.FindChild("Main Camera").gameObject.SetActive(true);
	}
}

It would help immensely if you copied the actual error message, and also what you've tried so far.

It looks like the spawnSpots array is empty, you should Debug.Log out the length of the array and come back to us with that number. It's probably not able to find any objects of type SpawnSpot. Just a guess though.

1 Answer

1

This is the new Quill18 tutorial, yes! (without looking at the link, just the code). It seems you havn’t attached the SpawnSpot script to your spawnSpot object, so it doesn’t find any. spawnSpots.Length returns zero, the random range returns zero, but there is no spawnspot at index zero, hence the out-of-range error.

Really this is not a beginner tutorial. You should do one of the many other tutorials by Quill18 (or any other) first. Why not do his first person shooter one first?

This is what I gathered at first glance, too

Yeah, simultaneous post! But you were correct even without knowing the tutorial, so upvote for you =]

Thanks, Quill is awesome :). i understand a bit about how programming works and blender and a tiny tiny bit about unity. so most makes sence. its just a couple of things like this that i dont get. yet. but i like jumping into the deep end, but yea i will go check out some of the other stuff, definitely.