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

Error

Discussion in 'Scripting' started by jepe400, Apr 28, 2018.

  1. jepe400

    jepe400

    Joined:
    Apr 28, 2018
    Posts:
    2
    So I m trying to make a local multiplayer fps prototype. There is a youtube video on this but couldn't find any help there:


    It throws this error:
    IndexOutOfRangeException: Array index is out of range.
    PlayerSetup.DisableComponents () (at Assets/Scripts/PlayerSetup.cs:17)
    PlayerSetup.Start () (at Assets/Scripts/PlayerSetup.cs:27)

    Here is my code:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3.  
    4. public class PlayerSetup : NetworkBehaviour {
    5.  
    6.     [SerializeField]
    7.     Behaviour[] componentsToDisable;
    8.  
    9.     [SerializeField]
    10.     string remoteLayerName = "RemotePlayer";
    11.  
    12.     Camera sceneCamera;
    13.     void DisableComponents()
    14.     {
    15.         for (int i = 0; 1 < componentsToDisable.Length; i++)
    16.         {
    17.             componentsToDisable[i].enabled = false;
    18.         }
    19.     }
    20.  
    21.     void Start ()
    22.     {
    23.         if (!isLocalPlayer)
    24.         {
    25.             // Disable components that should only be
    26.             // active on the player the we control
    27.             DisableComponents();
    28.             AssignRemoteLayer();
    29.         }else
    30.         {
    31.             // We are the local player: Disable the scene camera
    32.             sceneCamera = Camera.main;
    33.             if (sceneCamera != null)
    34.             {
    35.                 sceneCamera.gameObject.SetActive(false);
    36.             }
    37.            
    38.         }
    39.     }
    40.     void AssignRemoteLayer ()
    41.     {
    42.         gameObject.layer = LayerMask.NameToLayer(remoteLayerName);
    43.     }
    44.  
    45.    
    46.  
    47.  
    48.     void OnDisable ()
    49.     {
    50.         if (sceneCamera != null)
    51.         {
    52.             sceneCamera.gameObject.SetActive(true);
    53.         }
    54.  
    55.     }
     
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    First of all, your title is very unhelpful, it has nothing to do with the problem, change it
    Your problem actually is, that on line 15 you wrote 1 < componentsToDisable... instead of i < componentsToDisable...
     
  3. jepe400

    jepe400

    Joined:
    Apr 28, 2018
    Posts:
    2
    Sorry. And thank you so very much :)