1

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;
}

1 Answers1

0

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"

enter image description here

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.

Understanding the "preload scene" and "generally available items" in Unity:

Since concepts like "globals" and "singletons" are totally meaningless in Unity, what do you do for

  • "generally needed" things in a project,
  • which exists only once
  • such as say the networking, scoring, or AI,
  • or simple examples like "a common font"?
  • and which you will need to access from any number of scenes, game objects, and Components ??

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 Components 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.)

Fattie
  • 27,874
  • 70
  • 431
  • 719
  • "Statics are totally meaningless on a game object." I disagree with this statement in the way the user is creating a static variable in a class or a MonoBehaviour (not GameObject), and they aren't meaningless, they mean what they mean in any OOP. Just because Unity does not show it in inspector, it does not mean you can't use it. "Remember that unity is nothing but a whole lot of game objects with things" I don't think it describes Unity correctly. – E. Zacarias Oct 22 '20 at 14:43
  • Thanks a lot Fattie I didn't know i couldn't use static or singleton in Unity for GameObject eheh. Again tysm and good evening – Lorenzo Piccioli Oct 22 '20 at 14:52
  • hi @E.Zacarias - i'm afraid to report that your comment is **absolutely** incorrect, and may confuse others. The `Font` class simply can not be a static. (Try it.) NB, I explain this in great detail in the footnote, in italics. Please note that your comment about inspector variables is totally unrelated. (I happen to mention inspector variables, as a typical thing in Unity, but they have no relation to the fact that a `Font` cannot be a static.) Would you please note that you say "a MonoBehaviour (not GameObject)" - a MonoBehavior **cannot exist** other than as a game object component. – Fattie Oct 22 '20 at 15:16
  • 1
    @Fattie `you can not of course do that in any way with actual game objects, or anything at all associated with Unity and game objects` .. yes you can ... `public static GameObject instance; private void Awake (){ instance = gameObject; }` done ;) Of course you can do exactly this with a font that is what E.Zacarias ment ;) It totally depends on how you assign that static field. I agree if you mean this is a question purely related to `c#` and not Unity **specific** .. you can do `public static class Utils { public static Font MyFont; }` and assign and access `Utils.MyFont` wherever you want – derHugo Oct 22 '20 at 17:28
  • sure DH, but it's completely meaningless / will break / not be coherent. if your code fragment is in monobehavior Xyz, that Xyz could be a component on any number of gameobjects in any number of scenes (which come and go) (or never be on any gameobject). Regarding the question, the question is about Unity. c# has almost no connection to Unity, it's just something you (can) (for now) compile components in!! – Fattie Oct 22 '20 at 19:04
  • note, your final "conveince code", candy, you would in fact have to load that from ......... a preload scene! it is completely misdirecting since it's a global pointing at "the thing in unity which is sort of like a global". equivalently, what everyone does is just go Grid.networking. (in this example Grid.blah.someFont) as mentioned at the end of that linked Long Post by a handsome list member :) – Fattie Oct 22 '20 at 19:07
  • Wouldn't this be a prime use case for ScriptableObjects? – Immersive Oct 23 '20 at 02:43
  • And just because Unity's *Inspector* only cares about instance fields doesn't mean you cant roll your own custom editor to take care of your statics. – Immersive Oct 23 '20 at 02:45
  • And isn't DontDestroyOnLoad a singleton pattern? – Immersive Oct 23 '20 at 02:48
  • Hi @Immersive ; hmm, ScriptableObjects would seem to be completely unrelated here. Your comment about statics/custom inspector makes no sense as a static on a game object is meaningless. (It would be like saying in Photoshop "I just drew a line and now I will make it a Static" - there's just no connection.) DontDestroyOL has no connection, in any way, to singletons. It's just a component (behavior) you can add to game objects (like "Run" or "Walk" or "Glow" etc.) It's incredibly commonplace and trivial that in a Unity project you'll have (often 100s) of assets (such as the font mentioned). – Fattie Oct 23 '20 at 11:55