using non static variable in static function

Here’s my problem:

I want to use a normal variable within a static function.

Of course this doesn’t work unless the variable itself is static as well. My workaround so far has been to create a copy of that variable as a static variable, then assigning the normal variable to the static version in in Start or Update, and using the static version in my static function.

public float myNormalVar;
public static float MyStaticVar;

void Start() {
MyStaticVar = myNormalVar;
}

The other workaround I tried was turning my class into a singleton, and using a static property to return an instance of my normal variable:

public float myNormalVar;
public static float MyStaticVar { get{ return instance.myNormalVar; } }

But I don’t want to turn this class into a singleton, there’s never gonna be 2 instances of that class so using static variables works perfect for it.

I was wondering, am I doing this the right way or is there another way to use non-static variables within static functions?

Thanks for your help!

Stephane

A singleton is the way to go. You list a lot of contradictory “I want” without saying why, so it’s hard to give more advice. If your reason for wanting to use a normal variable is so that it shows in the inspector then a singleton is precisely the correct way to achieve that. It’s up to you whether you want the variables that do not need to appear in the inspector to be static or member variables accessed through the singleton.

As for “there’s never gonna be 2 instances of that class”, well yes, that’s always the case for singletons.