Accessing Global Variable Unique To Scene

Hi everyone,

I’m rather new to C# and coding in general. I’m working on a simple 2D sidescrolling game in which I have a bunch of blocks that act as surfaces you can stand on. These blocks are prefabs with a 2D sprite that I want to change the renderer color on depending on the level. I’m not really sure how to go about this. My first thought was that I could create an empty gameobject to use as a level manager. On that level manager object I would add a C# script that contains a bunch of level-specific global variables and make them public so I can edit them in the inspector.

But how would I go about creating an efficient way to have my blocks access a Color variable from that level manager and then changing the color of the sprite? I’ve been at this for hours and nothing seems to work.

Just create a script attached to an empty gameObject. Make sure it is a singleton:

public class LevelGlobals : MonoBehaviour{

public static LevelGlobals instance{get;private set;}

void Awake(){
instance=this;
}

public int myExampleGlobal;

}

Then you can access it anywhere:

LevelGlobals.instance.myExampleGlobal

But make sure this script exists in every level. Have fun :wink: