- Home /
Cannot convert 'UnityEngine.Object' to 'UnityEngine.Font'
I can usually resolved these kind of type conversion issues on my own, but I'm having a hard time figuring this one out.
I'm trying to assign a font at runtime to my GUIText.font, using Resources.Load (for the first time). My font is in "Resources/Fonts" and is called "Arial Black". Here's my current code below:
GameObject countDownGO = new GameObject("CountDownTimer");
countDownGO.transform.parent = GameObject.Find("HUD").transform;
GUIText countText = countDownGO.AddComponent<GUIText>();
countText.text = timer.ToString();
countText.font = Resources.Load("Fonts", typeof(Font));
The "countText.font = Resources.Load("Fonts", typeof(Font));" line above asserts with "Cannot implicitly convert 'UnityEngine.Object' to 'UnityEngine.Font', An explicit conversion exists (are you missing a cast?)"
I have tried the following with no luck:
countText.font = Resources.Load("Fonts", typeof(Font) as Font);
If someone could show me what I'm doing wrong it would be awesome :)
Also, is this the best way to assign a font at runtime?
Thanks for your time!
Stephane
Answer by ronronmx · Feb 17, 2011 at 06:57 PM
I just figured it out :)
countText.font = (Font)Resources.Load("Fonts/Arial Black", typeof(Font));
Now, I know you can load objects using the "Resources" folder, and also using "AssetBundles", but since I do not own Unity Pro, I cannot use "AssetBundles", is this correct? So is my only option using "Resources.Load" ?
Thanks for any help on this subject!