10

in my game I have a UI Text that I've called "label", and I want to set its font programmatically. I've tried to do this:

label.GetComponent<Text>().font="Arial";

I get an error because the font attribute doesn't want a string but a Font. So how can I set the font to Arial programmatically?

nix86
  • 2,837
  • 11
  • 36
  • 69

2 Answers2

24

This works:

label.GetComponent<Text> ().font = Resources.GetBuiltinResource(typeof(Font), "Arial.ttf") as Font;
nix86
  • 2,837
  • 11
  • 36
  • 69
1

try to create a public variable of type Font in the editor.

public Font myNewFont;

then you can do something like

label.GetComponent<Text>().font= myNewFont;

Not able to test it, but I think it should work, here's a very similar question... How to change Font type in Unity?

Community
  • 1
  • 1
Roberto Guajardo
  • 490
  • 1
  • 4
  • 15
  • But I want to use Arial. Not my font – nix86 Jun 16 '15 at 16:48
  • I think arial is the default value, but if not.. you can assign the font Arial in the myNewFotn variable or do : `label.GetComponent().font = Resources.GetBuiltinResource(typeof(Font), "Arial.ttf") as Font ` this is described in the other post that I've shared with you – Roberto Guajardo Jun 16 '15 at 16:51
  • By default the font is none – nix86 Jun 16 '15 at 16:56