Game works in editor but performance is bad or doesn't work in build

When I played the game I made in the editor, it ran fine.
klutzynecessarygermanspaniel
But when I export it to windows, the menu and the watermark scenes worked fine but when actually playing the game it the performance is bad or nothing is happening except for the updating of the UI.
pleasantverifiableambushbug
Please help

[EDIT]
When I ran the profiler (somebody suggested to use it) on the build, the scripts usage on the cpu is VERY HIGH. But I couldn’t figure out what was making it spike.
[EDIT 2]
When playing the game in build, other than the bad performance the music doesn’t sync as well even though it lines up fine in the editor. Here is the code for the music syncing:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Audio;

public class KeyClick : MonoBehaviour
{
    public KeyCode key;
    bool active = false;
    GameObject note;
    public SpriteRenderer rendererSprite;
    public Sprite pressedKey;
    public Sprite unpressedKey;
    public AudioSource music;
    void Update()
    {
        if (Input.GetKeyDown(key) && active)
        {
            add();
            Destroy(note);
        }
        if (Input.GetKeyDown(key) && active == false){
            minus();
        }
        if (Input.GetKeyUp(key))
        {
            rendererSprite.sprite = unpressedKey;
        }
        if (Input.GetKeyDown(kee))
        {
            rendererSprite.sprite = pressedKey;
        }
    }
//Void add and minus for the score
    void add(){
        scorekeeper.score += 5;
        canvasManager.notesClicked++;
    }
    void minus(){
        scorekeeper.score -= 2;
    }
    void OnTriggerEnter2D(Collider2D collision1)
    {
        active = true;
        if (collision1.gameObject.tag == "note"){
            note = collision1.gameObject;
        }
        //Music Sync Code
        if (collision1.gameObject.tag == "openingkey"){
            music.Play();
            note = collision1.gameObject;
        }
    }
    void OnTriggerExit2D(Collider2D collision)
    {
        active = false;
    }
}

Sorry what’s not working here? Both videos look fine as far as I can tell. What’s missing?

1 Like

There’s nothing missing, it’s the performance that’s the problem. When I play it in the editor, the music syncs up to the gameobjects but when exported that is when the problem happens. The gameobjects don’t sync to the music anymore.

If the problem is with the music syncing with the gameobjects, you probably should have included the music in the videos. But share the code you’re using to sync the gameobjects and music together, and maybe someone can see the problem.

Sorry, I should have included the audio with the videos as well! And will update the first post to include the music sync code.

You might consider disabling the “note” gameobjects instead of destroying them, and reusing them in a pool. This would reduce the amount of garbage generation / collection.

When profiling, you should be able to see exactly what method / part of your scripts are CPU intensive. That will give you the information you need to improve performance.

If your music clips are short notes (impossible to tell from the video), you may want to use PlayOneShot instead of Play if you expect the notes to overlap.

Will try object pooling, thank you for the suggestion! And I will learn how to use the profiler soon.