- Home /
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.
Hey, thanks for the reply. I haven't tried anything yet I really have no clue why its giving me this error... im a noob at this and its just a hobbie of $$anonymous$$e and i am fallowing this tutorial.
i have everything he put onto the code and i even doenloaded the file that he created it and went through it. still got the error. maybe its something to do with me having a newer version of unity? i just downloaded it. the version i have is 4.3.0f4
http://www.youtube.com/watch?v=oZ7TQjWhtEw&list=SPbghT7$$anonymous$$mckI7BDIGqNl_TgizCpJiXy0n9
the error code im getting it:
IndexOutOfRangeException: Array index is out of tange. networkmanager.Spawn$$anonymous$$yPlayer() filedir/networkmanager.cs:43
Please don't post comments as answers. Post comments by clicking the [add new comment] button, a window then open for you to type in. Answer fields are for answers only, as this is a knowledge base.
Here at Unity Answers, Answer means Solution, not Response.
Read this page : http://answers.unity3d.com/page/newuser.html
Please watch : http://video.unity3d.com/video/7720450/tutorials-using-unity-answers
I have converted this one for you.
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.
Answer by AlucardJay · Dec 12, 2013 at 09:18 PM
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?
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 program$$anonymous$$g 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.
Your answer
Follow this Question
Related Questions
Photon Network Instantiate Objects over Network 1 Answer
Get Photon View Id of gameobject. 2 Answers
hovering usernames with photon 1 Answer
photon networking and unity networking 1 Answer
Photon Networking and Team creation. 3 Answers