I need to assign a font to the variable m_StaticFont. The problem is that it need to be a static variable
private static Font m_StaticFont;
public static void updateData()
{
m_nameText.font = m_StaticFont;
}
I need to assign a font to the variable m_StaticFont. The problem is that it need to be a static variable
private static Font m_StaticFont;
public static void updateData()
{
m_nameText.font = m_StaticFont;
}
Unity is completely unconnected to OO programming.
Statics are totally meaningless on a game object.
The Font
would just be an inspector variable on some particular game object. (In very unusual cases (perhaps dynamic generation of fonts!) you can find it in the assets by name, see documentation, but usually you'd just link to the assets, which is what a Font is, using an inspector variable, as you do with every one of the 100s of assets in a Unity project.)
public Font happyFont;
Say you have a game object "Convenience"
Say you have a MonoBehavior
perhaps called Utility
and that Component
is on "Convenience". Say Utility
has the inspector variable Font
mentioned above.
class Utility: MonoBehavior
{
public Font happyFont;
}
Somewhere in the scene, you need "that font".
Simply find "Convenience", then Utility
and you have it.
example = utility.happyFont;
Note - if you don't know "how to find or link to" other game objects, that is an absolute basic in Unity, I'm guessing OP knows how to do that.
You can't have a "static" or "singleton" in Unity, it's totally meaningless.†
Since concepts like "globals" and "singletons" are totally meaningless in Unity, what do you do for
Component
s ??Fortunately the solution is dead simple. Unity forgot to include a "preload" or "generally available" Scene in Unity (they will finally add it one of these years).
For now, click one button to add one, couldn't be simpler.
Here is a long explanation of that very simple process:
https://stackoverflow.com/a/35891919/294884
† Note that, obviously, since, presently, C# is used for Component
s in Unity, you can of course, with say simply ints or floats, have a class (totally "unused" by Unity, i.e. no connection whatsoever to scenes or game objects), like this ..
class ExampleSettings
{
public static int versionNumber;
}
obviously you can refer to ExampleSettings.versionNumber
anywhere in the namespace. So that's a static. But it has utterly no connection "to Unity", you can not of course do that in any way with actual game objects, or anything at all associated with Unity and game objects. (Remember that unity is nothing but a whole lot of game objects with things (such as Font) that are part of game objects.)