Search Unity

Start function works on game startup but gives argument out of range error on scene reload

Discussion in 'Scripting' started by RummanOfficial, Apr 12, 2021.

  1. RummanOfficial

    RummanOfficial

    Joined:
    Jan 31, 2020
    Posts:
    2
    Hello everyone, I am relatively new to unity and am working on a mobile game. I have created a random level generator script which allows me to create infinite random levels using a combination of platform prefabs. In theory, this code would allow me to create the whole game using one scene which I would reload on level completion. The code works fine when I start the game in the editor, however, when I reload the scene using UnityEngine.SceneManagement, I get an argument out of range exception on line 40. I don't know what is causing the error. Please help...

    Code (CSharp):
    1. public GameObject startPlat, endPlat, heelPlat, heelPlat2, sandPlat, sandPlat2, waterPlat, jogPlat, jogPlat2;
    2.     public GameObject playerPlat, enemyPlat;
    3.     public List<GameObject> platPartList, currentParts;
    4.     public int levelLength;
    5.     PlayerNew player;
    6.     float zOffset;
    7.     GameObject prevPart;
    8.     UIManagerNew uimanNew;
    9.  
    10.     // Start is called before the first frame update
    11.     void OnEnable()
    12.     {
    13.         uimanNew = FindObjectOfType<UIManagerNew>().GetComponent<UIManagerNew>();
    14.         if (uimanNew.makeLevel)
    15.         {
    16.             levelLength = PlayerPrefs.GetInt("currentLvlLength");
    17.             player = FindObjectOfType<PlayerNew>().GetComponent<PlayerNew>();
    18.             if (player.heel)
    19.             {
    20.                 platPartList.Add(heelPlat);
    21.                 platPartList.Add(heelPlat2);
    22.             }
    23.             if (player.sand)
    24.             {
    25.                 platPartList.Add(sandPlat);
    26.                 platPartList.Add(sandPlat2);
    27.             }
    28.             if (player.water)
    29.                 platPartList.Add(waterPlat);
    30.             if (player.jog)
    31.             {
    32.                 platPartList.Add(jogPlat);
    33.                 platPartList.Add(jogPlat2);
    34.             }
    35.             GameObject temp = Instantiate(startPlat, new Vector3(0f, 0f, zOffset), startPlat.transform.rotation);
    36.             temp.transform.SetParent(playerPlat.transform);
    37.             zOffset += 7.5f;
    38.             for (int i = 0; currentParts.Count < levelLength; i++)
    39.             {
    40.                 int rand = Random.Range(0, platPartList.Count);
    41.                 //Debug.Log(platPartList.Count);
    42.                 temp = platPartList[rand];
    43.                 if (temp != prevPart)
    44.                 {
    45.                     GameObject temp2 = Instantiate(temp, new Vector3(0f, 0f, zOffset), startPlat.transform.rotation);
    46.                     temp2.transform.SetParent(playerPlat.transform);
    47.                     zOffset += 7.5f;
    48.                     currentParts.Add(temp2);
    49.                     prevPart = temp;
    50.                 }
    51.             }
    52.             temp = Instantiate(endPlat, new Vector3(0f, 0f, zOffset), endPlat.transform.rotation);
    53.             temp.transform.SetParent(playerPlat.transform);
    54.             temp = Instantiate(playerPlat, new Vector3(-3f, 0f, 0f), Quaternion.identity);
    55.             temp.transform.SetParent(enemyPlat.transform);
    56.  
    57.         }
    58.  
    59.     }
     
  2. RummanOfficial

    RummanOfficial

    Joined:
    Jan 31, 2020
    Posts:
    2
    Sorry guys, false Alarm. I found the solution. What happened was that I had multiple start functions on multiple scripts. My concept required them to execute in a certain order (which they didn't!!). I found out the handy "Script Execution Order" setting in unity Player Settings which solved my problem.
     
    Joe-Censored likes this.