IndexOutOfRangeException: Index was outside the bounds of the array.

The full error is

"
IndexOutOfRangeException: Index was outside the bounds of the array.
GroundSpawnerScript.PositionThePlayer () (at Assets/scripts/GroundColliderScripts/GroundSpawnerScript.cs:249)
GroundSpawnerScript.Start () (at Assets/scripts/GroundColliderScripts/GroundSpawnerScript.cs:46)

"

Honestly, I think I’m just being dumb, but I cant seem to find the fix to the error

Here is the code:

    void Start()
    {

        controllX = 0;

        // the y position of the first ground is going to start from center
        float center = Screen.width / Screen.height;

        center += 1.0f;

        CreateGround(center);

        for (int i = 0; i < collectables.Length; i++)
        {
            collectables[i].SetActive(false);
        }

        // getting players script
        playerScript = GameObject.Find("Player").GetComponentInParent<Player>();

        PositionThePlayer(); // this is line 46 which it seems to have an issue with.


    }
    void PositionThePlayer()
    {

        // getting back ground
        GameObject[] deadlyGround = GameObject.FindGameObjectsWithTag("Deadly");

        // getting ground in game
        GameObject[] groundInGame = GameObject.FindGameObjectsWithTag("Ground");

        for (int i = 0; i < deadlyGround.Length; i++)
        {

            if (deadlyGround[i].transform.position.y == 0)
            {

                Vector3 t = deadlyGround[i].transform.position;

                deadlyGround[i].transform.position = new Vector3(groundInGame[0].transform.position.x,
                                                               groundInGame[0].transform.position.y,
                                                               groundInGame[0].transform.position.z);

                groundInGame[0].transform.position = t;

            }

        }

        Vector3 temp = groundInGame[0].transform.position; // here is line 249

        for (int i = 1; i < groundInGame.Length; i++)
        {

            if (temp.y < groundInGame[i].transform.position.y)
                temp = groundInGame[i].transform.position;

        }


        // positioning the player above the cloud
        playerScript.transform.position = new Vector3(temp.x, temp.y + 0.8f, temp.z);


    }


}

My guess is that your CreateGround method either doesn’t tag objects it creates, or it deactivates the game objects. FindObjectsWithTags only finds active game objects. So, line 249 tries to access an empty array. It is able to get there because the for loop before that doesn’t do anything, since the other array is probably also empty, resulting in a length of 0.