Cannot change <FontName> </FontName> of file .spritefont

I am following this tutorial -www.youtube.com/watch?v=jr8sktSwn7E&index=5&list=PLHJE4y54mpC5hrlDv8yFHPfrSNhqFoA0h

In tutorial 5, i’m already created file Obitron.spritefont like this.

When i changed FontName from Arial to
Obitron

after run the program i got these error

Error The command ““C:\Program Files (x86)\MSBuild\MonoGame\v3.0\Tools\MGCB.exe” /@:“C:\Users\Tkaewkunha\Documents\Visual Studio 2015\Projects\Game1\Game1\Content\Content.mgcb” /platform:Windows /outputDir:“C:\Users\Tkaewkunha\Documents\Visual Studio 2015\Projects\Game1\Game1\Content\bin\Windows” /intermediateDir:“C:\Users\Tkaewkunha\Documents\Visual Studio 2015\Projects\Game1\Game1\Content\obj\Windows” /quiet” exited with code 1. Game1

and this

Error Index was out of range. Must be non-negative and less than the size of the

This is the class that calling SpriteFont

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace Game1
{
    public class Image
    {
        public float Alpha;
        public string Text, FontName, Path;
        public Vector2 Position, Scale;
        public Rectangle SourceRect;

        [XmlIgnore]
        public Texture2D Texture;

        Vector2 origin;
        ContentManager content;
        RenderTarget2D renderTarget;
        SpriteFont font;

        public Image()
        {
            Path = String.Empty;
            FontName = "Fonts/Obitron";
            Position = Vector2.Zero;
            Scale = Vector2.One;
            Alpha = 1.0f;
            SourceRect = Rectangle.Empty;
        }
        public void LoadContent()
        {
            content = new ContentManager(
                ScreenManager.Instance.Content.ServiceProvider, "Content");
            if (Path != String.Empty)
                Texture = content.Load<Texture2D>(Path);

            font = content.Load<SpriteFont>(FontName);

            Vector2 dimensions = Vector2.Zero;

            if (Texture != null)
                dimensions.X += Texture.Width;

            dimensions.X += font.MeasureString(Text).X;

            if (Texture != null)
                dimensions.Y = Math.Max(Texture.Height, font.MeasureString(Text).Y);
            else
                dimensions.Y = font.MeasureString(Text).Y;

            if (SourceRect == Rectangle.Empty)
                SourceRect = new Rectangle(0, 0, (int)dimensions.X, (int)dimensions.Y);

            renderTarget = new RenderTarget2D(ScreenManager.Instance.GraphicsDevice,
                (int)dimensions.X, (int)dimensions.Y);

            ScreenManager.Instance.GraphicsDevice.SetRenderTarget(renderTarget);
            ScreenManager.Instance.GraphicsDevice.Clear(Color.Transparent);
            ScreenManager.Instance.SpriteBatch.Begin();
            if (Texture != null)
                ScreenManager.Instance.SpriteBatch.Draw(Texture, Vector2.Zero, Color.White);
            ScreenManager.Instance.SpriteBatch.DrawString(font, Text, Vector2.Zero, Color.White);
            ScreenManager.Instance.SpriteBatch.End();
  
            Texture = renderTarget;
            ScreenManager.Instance.GraphicsDevice.SetRenderTarget(null);
        }
        public void UnloadContent() {
            content.Unload();
        }
        public void Update(GameTime gameTime)
        {

        }
        public void Draw(SpriteBatch spriteBatch)
        {
            origin = new Vector2(SourceRect.Width / 2,SourceRect.Height/2);
            spriteBatch.Draw(Texture, Position + origin, SourceRect, Color.White * Alpha, 
                0.0f, origin, Scale, SpriteEffects.None, 0.0f);

        }
    }
}

Any suggestion?
Thank you.

Have you got the ttf file in the same location as the .spritefont?

Open the ttf, does the font name match the name you are writing?

1 Like

This is files in my folder Content/Fonts/

free image host

I thought you needed the ttf file also in there.

But I guess if it has built maybe not.

Does your content pipeline build and it is running the game that fails?

If i dont chage FontName tag (Keep it Arival) i got this error instead.

[img=https://s26.postimg.org/nsim87wwp/Capture.png]

Thats because Text is null.

you declare at the top a string called Text, but don’t assign it anything.

Did you download this Obitron font?

No, i just created new SpriteFont file and change FontName.

I can get the same error “Index was out or range” if I change a SpriteFont file to try to use a Font I do not have installed on my system.

Is Obitron installed on your system?

I think you need to download this Obitron’s file that is in a .ttf format and put that in the Fonts directory you have when you build your content pipeline.

1 Like

Thank you very much. I will try it right now.

Thank you very much.

As mentioned above, in your code sample you don’t set the Text member to a string value. So it will still be null and that is not a valid string.

1 Like

Thank you ,i am already set it now.