I am doing this:

string fontName = "freespin_big";

TextAsset xml = Resources.Load<TextAsset>(fontName);
Texture2D image = Resources.Load<Texture2D>(fontName);

Font font = Resources.Load<Font>(fontName);
if (font == null)
    font = new Font();
    AssetDatabase.CreateAsset(font, "Assets/Resources/" + fontName);

In Resources, we have freespin_big.png and freespin_big.xml. I am planning to import the png and xml and create a font. So I check if the font object is there, if not create it.

But I get this error if the font does not exist yet:

InvalidCastException: Cannot cast from source type to destination type.

If I try to do this:

Font font = Resources.Load<Font>("blorg");

Where no files are named "blorg", I get null as I expect.

Shouldn't Resources.Load return null if it finds no fonts with the right name?


Font font = Resources.Load("fontname",typeof(Font)) as Font;

Using this form, instead of the form with in it, works.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.