Script working in Unity editor but not post-build product

Hi, I wrote code to make the wings on my bird flap each time the player presses a space bar. This seems to work fine in the editor but after I build the game, they freeze in the final product. I’m aware my code is far from stellar since I’ve only just begun, but I’d appreciate any help. Thanks!

Streamable link to video: Watch 2024-07-29 20-37-08 - Trim | Streamable

Script for my wings:

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

public class WingAnimation : MonoBehaviour
{

    public BirdScript bird;
    private int flapTimer;
    private int flapCoolDown = 200;
    private bool flapCooled = false;
    private bool justFlapped = false;

    // Start is called before the first frame update
    void Start()
    {
        bird = GameObject.FindGameObjectWithTag("Bird").GetComponent<BirdScript>();
    }

    // Update is called once per frame
    void Update()
    {

        if (Input.GetKeyDown(KeyCode.Space) && bird.birdIsAlive)
        {
            if (flapCooled == false)
            {
                transform.Rotate(-180, 0, 0);
                flapCooled = true;
                flapTimer = flapCoolDown;
                justFlapped = true;
                //Debug.Log("First flap");
            }
            else
            {
                flapTimer = flapCoolDown;
            }
            
        }

        if (flapTimer == 0 && justFlapped)
        {
            transform.Rotate(-180, 0, 0);
            flapCooled = false;
            justFlapped = false;
            //Debug.Log("Second flap");
        }
        
        if (flapTimer != 0)
        {
            flapTimer -= 1;
        }

    }
}

You probably want to locate the player.log file and see if any errors are being thrown. You can work out where it is here: Unity - Manual: Log files

That said, if this script is on the same, child, or parent game object to BirdScript, you should just reference it directly via the inspector and not use FindGameObjectWithTag .

Didn’t know I could use the inspector to drag and drop script from parent objects, thanks! It also turns out the problem was my timer. The game by itself ran at a lower frame rate than the game in the editor which messed up my timer. I should have multiplied my timer by Time.deltatime. Didn’t think it would be that big of a difference in the end but ig that came back to bite me lol

Change the Update function in your script to FixedUpdate. This function gets called at a fixed interval regardless of frame rate, making it more reliable for time-based actions like flapping wings.

Implement a custom event in your BirdScript magistv, that gets triggered when the bird is alive and the spacebar is pressed like metro, your WingAnimation script can listen for this event and flap the wings. This removes the dependency on Update being called every frame. So, that’s it.

Sorry but this is not true. FixedUpdate is for physics only. And it plays catch-up with Update. It’s called before Update if enough time has passed, and can be called multiple times before Update sometimes.