Calling class outside of an object

I have test CODE #1 to load strategy game information for map terrain and units. At anytime a player can exit to the main menu to load a new game. CODE #1 is in a separate class which requires no object.

CODE #1

using UnityEngine;
using System.Collections;

public class LoadCGN {
	
	//loads terrain data from file into array from random numbers.  Later this will be a file read.
	public void TerrainMap () {
		for(int x=0; x < Game.fullMapDimX; x++) {
			for(int y=0; y < Game.fullMapDimY; y++) {
			Game.terrainMap[x,y] = Random.Range(1,480);
			}
		}
	}
	
	public void UnitMap () {
		for(int x=0; x < Game.fullMapDimX; x++) {
			for(int y=0; y < Game.fullMapDimY; y++) {
			Game.unitMap[x,y] = Random.Range(1,8);
			}
		}
	}	
	
}

CODE #2 is suppose to load the data from the CODE#1 above and create the map mesh.

CODE #2

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

[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]

public class TerrainMeshBuilder : MonoBehaviour
{
		//other code here
		LoadCGN.TerrainMap ();    //load map data
		//build mesh code here
}

I keep getting the error below and I can’t find a “laymen’s terms” answer on it via my research.

Assets/Scripts/TerrainMeshBuilder.cs(58,25): error CS0120: An object reference is required to access non-static member `LoadCGN.TerrainMap()’

Nevermind I figured it out myself accidentally.

public static void TerrainMap ()

public static void UnitMap ()

instead of

public void TerrainMap ()

public void UnitMap ()

Alternately, and without the use of statics-

LoadCGN cgn = new LoadCGN();
cgn.TerrainMap();

You can also make a reference variable to an object with the script and use,

var myObject; //assign publicly through inspector programmatically find the object in the scene using, GameObject.FindGameObjectWithTag(“Tag”);

myObject.GetComponent().CallMethod();

Thanks guys. I put both those in my notes. Procedural programmer getting use to OOP. Slowly I’m getting it.

The main thing is - you said LoadCGN “doesn’t require an object” when, in actuality, it is an object. :slight_smile: