Player can reference Serialised Script in on scene but not in other

Hi,

I am making a shooter and to make the cooldown for a gun i created a serialisable script (so i could use it in other places as well) which looks like this:

using UnityEngine;

[System.Serializable]
public class Cooldown
{
    [SerializeField] private float cooldownTime;
    float _nextFireTime;

    public bool IsCoolingDown => Time.time < _nextFireTime;
    public void StartCooldown() => _nextFireTime = Time.time + cooldownTime;
}

this worked when i used it in the Player’s script in my Test Scene, but as soon as i turned the player into a prefab and added it to a different scene, it stopped working. the script for the gun looks like this (i used Unity’s new input system btw):

void OnShoot(InputValue value)
    {
        shootPressed = value.isPressed;

        if (shootCooldown.IsCoolingDown) return;

        Vector3 dir = new Vector3(transform.forward.x, 0, transform.forward.z).normalized;
        GameObject bullet = Instantiate(bulletPrefab, shootOrig.position, Quaternion.identity);
        BulletController bulletController = bullet.GetComponent<BulletController>();
        bulletController.target = shootOrig.position + dir * bulletHitMiss;
        bulletController.hit = false;
        bulletController.bulletSpeed = bulletSpeed;

        shootCooldown.StartCooldown();

    }

the cooldown works for the enemies who use the same script in the same scene, it only breaks for the player in that scene. it also doesn’t work in other scene that add the player in other than the test scene which doesnt have anything special nor should affect the cooldown script in any way. i have looked everywhere and have no idea why this is happening, so i could really use the help

What do you mean by “it breaks”?
Add some Debug.Log statements in the code
When you say “it breaks” and “doesn’t work”, no one will know what you’re talking about.

Sorry,

normally it should check whether the cooldown is cooling down in line 5 but in this case it skips that line entirely and goes straight to shooting so it just lets me shoot as many times as i like

Verify the cooldownTime is set in the prefabs, that it’s not 0.

the prefab has it set to 0.5 seconds but for some reason it just bypasses it. i did a bit of debugging on whether its cooling down and it will say false but still let me shoot

I wasn’t aware you could use [SerializeField] in classes that don’t inherit from UnityEngine.Object. I created a project in Unity 2023.1, created the below class, turned it into a prefab, and it’s working just fine.

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

public class Test : MonoBehaviour
{
    public Cooldown cooldown;

    private void Update()
    {
        if (cooldown.IsCoolingDown) return;

        Debug.Log("Foo.");

        cooldown.StartCooldown();
    }
}

i didn’t think to create a new project so i might try to fix it using that. referring to your post though it only stopped working after i brought the prefab into a new scene, in which it skips “if (cooldown.IsCoolingDown) return;”. i don’t think the problem comes from turning it into a prefab. it could be that it just can’t find it when brought into a new scene but i dont get any console errors or null reference exceptions.

I tried that by deleting it from the scene and recreating it. I didn’t try deleting the scene and creating a new one.

ok so i found how to to solve it but it doesn’t make sense.

i created and add the player prefab. i then slowly added everything that was in my test scene until it worked. i dont know why but for some reason the cooldown for the player only works until i bring in the enemy prefab that i had coded before.

the enemy does not have any code that should interfere with the player and looking through i have not found why this happens. nevertheless i can probably work my way around since the player will never be in a scene without a single enemy but i’m still confused why or how this happened.

thank you for your help anyways.