Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Game Development How to Make a Video Game Score, Enemies, and Game State Script the Score Counter

Frames per second

Hi all, So I just used the example of adding the Score text field to to the frog game to make a Frames per second Text field on the same canvas.

Here's the code:

public class FrameAverage : MonoBehaviour {


    private float frames;
    private int loops;
    private int oneSecond;
    private Text textBox;
    [SerializeField]
    private bool logFrames;

    // Use this for initialization
    void Start () {
        frames = 0f;
        loops = 0;
        oneSecond = 1;
        textBox = GetComponent<Text> ();
    }

    // Update is called once per frame
    void Update () {
        // Check if Logging:
        if (logFrames) {
            // Log average fps for last 10 frames to the screen:
            if (loops == 10) {
                frames /= 10f;
                textBox.text = "Frames: " + frames;
                frames = 0f;
                loops = 0;
            } else {
                frames += oneSecond / Time.deltaTime;
                loops++;
            }
        } else {
            // If logging unchecked, clear screen:
            textBox.text = "";
        }

    }
}

My question is: When I run this, I get frame rates of about 18. Now the laptop I study on is in no way powerful at all - built in intel graphics, 8GB RAM, ultrabook i5 CPU, but it can still play lots of 5 year old games at well over 50fps lol, and this is a pretty basic scene with not a lot of things to draw so that made me wonder:

  • Is my script wrong - I'm getting fps by dividing 1 second by the time the last frame took?
  • Is Unity just quite slow when you want to play-test your game?
  • Is something in this scene more demanding than I assume it is?

Thanks in advance for any help or clarification :)

Jon