C# Error CS1519: Unexpected Symbol "" in class, struct, or interface member declaration

I have looked at other questions regarding this topic, and they don’t seem to fit the particulars of mine.

I am attempting to programatically create a terrain asset, and I was folowing the answer from this post: Creating TerrainData.Asset programatically - Unity Answers

using UnityEngine;
using System.Collections;

public class GameManager : MonoBehaviour {
	ArrayList< GameObject > gameObjects = new ArrayList<GameObject>();
	
	TerrainData _terrainData = new TerrainData ();
	const int size = 513;
	
	
	_terrainData.heightmapResolution = size;
	_terrainData.size = new Vector3 ( 2000, 600, 2000 );
	
	_terrainData.baseMapResolution = 512;
	_terrainData.SetDetailResolution ( 1024, _terrainData.detailResolutionPerPatch );
	
	AssetDatabase.CreateAsset ( _terrainData, "Assets/New Terrain.asset" );
}

From this code, it generates an error CS1519 for just about everything after the const declaration. I think the problem has to do with the _terrainData variable. The reason for this is that the auto complete doesn’t recognize the variable after it was declared. If I type the full name and use the dot operator on it, it then starts to use the auto complete, but the whole line remains underscored red. What am I missing here?

Errors in above script:

  1. ArrayList is non generic. Change it to

    List

from System.Collections.Generic namespace.

  1. You need to wrap initialization part of the code in some method, e.g. Awake or Start.

  2. You need to use UnityEditor namespace

After corrections, your script should look like this:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
 
public class GameManager : MonoBehaviour {
    List<GameObject> gameObjects = new List<GameObject>();
 
    TerrainData _terrainData = new TerrainData ();
    const int size = 513;

    void Awake()
    {
        _terrainData.heightmapResolution = size;
        _terrainData.size = new Vector3(2000, 600, 2000);

        _terrainData.baseMapResolution = 512;
        _terrainData.SetDetailResolution(1024, _terrainData.detailResolutionPerPatch);

        AssetDatabase.CreateAsset(_terrainData, "Assets/New Terrain.asset");
    }
}