How do I fix error CS8803 (game cameras)

I was coding game cameras in a tutorial but I got error CS8803 (Top-level statements must precede namespace and type declarations). Here is the code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameController : MonoBehaviour {

    public GameObject[] gameCameras;

    private int gameCameraIndex = 0;

    // Start is called before the first frame update
    void Start() {
        FocusOnCamera (gameCameraIndex);
    }

    // Update is called once per frame
    void Update() {
     if (Input.GetMouseButtonDown(0)) {
            ChangeCamera (1);
        }
    }

    void FocusOnCamera (int index) {
        for (int i = 0; i < gameCameras.Length; i++) {
            gameCameras [i].SetActive (i == index);
        }
    }
}

void ChangeCamera (int direction) {
    gameCameraIndex += direction;

    if (gameCameraIndex >= gameCameras.Length) {
        gameCameraIndex = 0;
    }

    if (gameCameraIndex <= 0) {
        gameCameraIndex = gameCameras.Length - 1;
    }

    FocusOnCamera (gameCameraIndex);
}

How do i fix this?

You’re just making typing mistakes.

You can fix all your own typing mistakes with the help of the errors.

Go look where you go this. You haven’t copied it correctly. You have put some of your code outside of your class.

As C# specifies, 100% of all code must be within a class.

Two steps to tutorials:

  1. do them perfectly, to the letter (zero typos, including punctuation and capitalization)
  2. stop and understand each step what is going on, otherwise you’re just wasting time.

If you just check through your brackets { and } you should be able to see that you closed your class on line 28 but then have a method ChangeCamera afterwards. This method needs to be inside your class, so just move it so that it is.