Error CS0120

I’m kinda new to C# programming, but I’ve done extensive C/C++ programming. My problem seems really simple, but every solution I find online doesn’t work. I get the Error CS0120

Error CS0120: An object reference is required for the non-static field, method, or property ‘SewerRooms.r’ (CS0120) (Assembly-CSharp-firstpass)

What I’m trying to create is a text game. The SewerRooms is the file I’m using to set up the rooms to explore. I have a seperate class file: GlobalVars, to handle the “global” variables. The entire GlobalVars file is as follows:

    using UnityEngine;
using System.Collections;

public class GlobalVars : MonoBehaviour {

	private static RoomHandler GameRooms;
	
	public static RoomHandler GetRooms
	{
		get {
			if( GameRooms == null )
			{
				GameRooms = new RoomHandler();
			}
			return GameRooms;
		}
	}
}

I’ve tried the class as static, I’ve tried it the way its posted without being static. I’ve tried accessing the variable directly, and I’ve tried, as shown, to use a property with a singleton.

In the SewerRooms file I try to call the GetRooms property like so:

public class SewerRooms : MonoBehaviour {
	
	RoomHandler r;
	
	// Use this for initialization
	public static void Start () {
		r = GlobalVars.GetRooms;
...

From that point, and the later usage of the variable ‘r’, I get the CS0120 error. Any help would be appreciated.

The way you declared SewerRooms.Start implies that it can be called without an instance of SewerRooms. However, r does.

The function Start, when you inherit from MonoBehaviour, is called by Unity at the proper moment (when the scene starts, on all the enabled script attached to an activated game object). You don’t need it to be static or public.

As for the class GlobalVar, unless you intend to attach it to a gameObject for holding resources, or to implement Update, or to use coroutines on it, you don’t need to inherite from anything.

using UnityEngine;
using System.Collections;

public class boxa : MonoBehaviour {
Vector3 vec1;

// Use this for initialization
void Start () {
	vec1 = Transform.localScale;
	Debug.log(vec1.x);
	Debug.log(vec1.y);
	Debug.log(vec1.z);
}

// Update is called once per frame
void Update () {

	Transform.localScale = vec1;


}

}