Author Topic: Change font from script?  (Read 2779 times)

jmansa

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 11
    • View Profile
Change font from script?
« on: April 15, 2014, 02:47:25 AM »
I am trying to build an GameObject from script with NGUI components attached but am having some issues with font?

How do I tell what font to use?

Here is how i am trying to do:
  1. for(int i = 0;i < 3;i++){
  2.                         GameObject test = new GameObject("TEST"+i);
  3.                         test.AddComponent<UIDragScrollView>();
  4.                         UILabel countryName = test.AddComponent<UILabel>();
  5.                         countryName.font = MyriadProBold;
  6.                         countryName.text = "Just testing";
  7.                         test.AddComponent<BoxCollider>();
  8.                         test.AddComponent<UISprite>();
  9.  
  10.                         test.AddComponent<LanguageChooser>();
  11.                 }

Any help is appreciated.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Change font from script?
« Reply #1 on: April 15, 2014, 09:28:28 AM »
1. You are creating a random object in the middle of nowhere.
2. You are not setting its layer.
3. You are not parenting it to anything, and then you add a label to it. Since it's not parented, it forces NGUI to re-parent it to the first valid panel. All NGUI objects must be underneath a panel.

So to fix it:

Use NGUITools.AddChild or NGUITools.AddWidget, specifying a parent object right away. All 3 problems disappear.