Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Third Party [Solved] PhotonNetwork IndexOutOfRangeException

Discussion in 'Multiplayer' started by ManAmazin, Oct 19, 2014.

  1. ManAmazin

    ManAmazin

    Joined:
    Aug 7, 2013
    Posts:
    246
    Hey guys im having a IndexOutOfRangeException when spawning player in game..

    while learning some basic photon networking i followed some quil18 tutorials and the map was already loaded when spawning in to keep things simple

    ive now created menus and such and it seems to bork the spawn when loading the scene

    Code (CSharp):
    1.  
    2. SpawnSpot mySpawnSpot = spawnSpots[Random.Range(0, spawnSpots.Length)];
    3.         GameObject myPlayerGO = (GameObject)PhotonNetwork.Instantiate("PlayerController", mySpawnSpot.transform.position, mySpawnSpot.transform.rotation, 0);

    error/Exception points to this Line:

    Code (CSharp):
    1. SpawnSpot mySpawnSpot = spawnSpots[Random.Range(0, spawnSpots.Length)];
     
  2. wrstscrnnm6

    wrstscrnnm6

    Joined:
    Jan 31, 2011
    Posts:
    20
    Random.Range is inclusive/inclusive so if your spawnSpots array has 5 entires.

    Code (CSharp):
    1.  Random Range(0, spawnSpots Length)
    Will sometimes spit out a 5 which is greater than your max index (which go from 0 - 4)
    Without seeing all of your code and your exact error messages I can say for sure but I suspect the fix should be something as simple as this:

    Code (CSharp):
    1.  Random Range(0, spawnSpots Length - 1)
     
  3. ManAmazin

    ManAmazin

    Joined:
    Aug 7, 2013
    Posts:
    246
    man I forgot all about the "-1" when it comes to random.range but unfortunately that did not work the screen shot below shows the exact error.

    http://gyazo.com/c93ee05b9a59847f878bd34503281281


    Spawning a player occurs in OnJoinedRoom() and is called from a UI button I am posting my network code below please keep in mind a lot of this is placeholder as it is from a tutorial.

    Thanks in advance for the help @wrstscrnnm6

    Code (CSharp):
    1. public class NetworkManager : MonoBehaviour
    2. {
    3.     public static NetworkManager Instance;
    4.     public GameObject StandbyCamera;
    5.     SpawnSpot[] spawnSpots;
    6.     public bool OfflineMode = false;
    7.     bool Connecting = false;
    8.  
    9.  
    10.     public string sceneLevel;
    11.  
    12.     //Handle New UI declarations
    13.     Text _UserName;
    14.  
    15.     void Start()
    16.     {
    17.         spawnSpots = GameObject.FindObjectsOfType<SpawnSpot>();
    18.         Instance = this;
    19.         DontDestroyOnLoad(gameObject);
    20.         PhotonNetwork.player.name = PlayerPrefs.GetString("Username", "YourANoob");
    21.     }
    22.  
    23.     public void ConnectToPhoton()
    24.     {
    25.         PhotonNetwork.ConnectUsingSettings("alpha 0.003");
    26.     }
    27.  
    28.     void OnDestroy()
    29.     {
    30.         PlayerPrefs.SetString("Username", PhotonNetwork.player.name);
    31.     }
    32.  
    33.  
    34.  
    35.     void OnGUI()
    36.     {
    37.         GUILayout.Label(PhotonNetwork.connectionStateDetailed.ToString());
    38.  
    39.  
    40.         if (PhotonNetwork.connected == false && Connecting == false)
    41.         {
    42.             if (GUILayout.Button("Single Player"))
    43.             {
    44.                 Connecting = true;
    45.                 PhotonNetwork.offlineMode = true;
    46.                 OnJoinedLobby();
    47.  
    48.             }
    49.  
    50.             GUI.Label(new Rect(150, 20, 120, 35), "UserName :");
    51.             PhotonNetwork.player.name = GUI.TextField(new Rect(230, 20, 200, 35), PhotonNetwork.player.name);
    52.  
    53.             if (GUILayout.Button("Multiplayer Player"))
    54.             {
    55.                 Connecting = true;
    56.                 ConnectToPhoton();
    57.             }
    58.         }
    59.     }
    60.  
    61.     void OnJoinedLobby()
    62.     {
    63.         PhotonNetwork.JoinRandomRoom();
    64.     }
    65.  
    66.     void OnPhotonRandomJoinFailed()
    67.     {
    68.         PhotonNetwork.CreateRoom(null);
    69.     }
    70.  
    71.     void OnJoinedRoom()
    72.     {
    73.         Connecting = false;
    74.         PhotonNetwork.LoadLevel("DeveloperArena");
    75.     }
    76.  
    77.     public void SpawnMyPlayer()
    78.     {
    79.         if (spawnSpots == null)
    80.         {
    81.             Debug.LogError("Dude Seriously??");
    82.             return;
    83.         }
    84.  
    85.         SpawnSpot mySpawnSpot = spawnSpots[Random.Range(0, spawnSpots.Length - 1 )];
    86.         GameObject myPlayerGO = (GameObject)PhotonNetwork.Instantiate("PlayerController", mySpawnSpot.transform.position, mySpawnSpot.transform.rotation, 0);
    87.  
    88.         StandbyCamera.SetActive(false);
    89.  
    90.         //Enable and disable components so each player controls itself
    91.         ((MonoBehaviour)myPlayerGO.GetComponent("MyMouseLook")).enabled = true;
    92.         ((MonoBehaviour)myPlayerGO.GetComponent("PlayerController")).enabled = true;
    93.         myPlayerGO.transform.FindChild("FirstPersonCharacter").GetComponent<Camera>().enabled = true;
    94.         myPlayerGO.transform.FindChild("FirstPersonCharacter").FindChild("Guns").FindChild("CartoonSMG").GetComponent<Weapon_Shooting>().enabled = true;
    95.     }
    96. }
    97.        
    98.  
    99.  
    100.  
    101.  
     
  4. wrstscrnnm6

    wrstscrnnm6

    Joined:
    Jan 31, 2011
    Posts:
    20
    To clarify: Is it throwing the error when the scene loads or after the scene loads and you hit the button?
     
  5. ManAmazin

    ManAmazin

    Joined:
    Aug 7, 2013
    Posts:
    246
    As of now it throws the error after the scene loads and when hitting the spawn button
     
    Last edited: Oct 20, 2014
  6. ManAmazin

    ManAmazin

    Joined:
    Aug 7, 2013
    Posts:
    246
    either way tho yields the same results for testing player spawned in as soon as scene started , i changed it to button as of yesterday and still does same thing
     
  7. ManAmazin

    ManAmazin

    Joined:
    Aug 7, 2013
    Posts:
    246
    @tobiass i messaged you not sure if you got it as im sure you receive alot a day, any ideas on above problem?
     
  8. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,062
    You can debug these kinds of issues by logging the .length of the array and the index you want to access. Make both variables, so you can log and then still use the values (one of them being random).
    If the array is public, it must be set in the Inspector. Make sure it has some values.
     
  9. ManAmazin

    ManAmazin

    Joined:
    Aug 7, 2013
    Posts:
    246
    i figured it out: following the tutorial of quil18 multiplayer he didnt setup different scenes to lead into game play he did the setup and all that in the map so everything just worked...when i veered away from his method and did my own i didnt take into account that the Start() function needed to be reworked as the connection to photon servers started in a different scene in short:

    Code (CSharp):
    1.  void Start()
    2.     {
    3.         spawnSpots = GameObject.FindObjectsOfType<SpawnSpot>();
    4.     }
    5.  
    this need changing since the script is initialized in a different menu where there are no Spawnspots to find..so by the time it gets to the actual map its not going to register the spawnspots array as the script has a donotdestroyonload meaning the Start() function wont be called again..


    so i Updated it to this code here:

    Code (CSharp):
    1. void Update()
    2. {
    3.      if(Application.Loadedlevel == 5)
    4.       {
    5.           //Then enter the spawnspot code here
    6.        }
    7.  
    8. }

    and that solved it
     
  10. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,062
    Oh, mean!
    With the 5 being hardcoded, you will run into trouble when the number of scenes changes in the future.
    Maybe you can add a special script to only that scene which should do the spawnspot stuff.
     
  11. ManAmazin

    ManAmazin

    Joined:
    Aug 7, 2013
    Posts:
    246
    yea i agree it just for my testing phase to solve that error the spawn code will definitely only reside in the map scene and not network script.......thanks @tobiass and @wrstscrnnm6