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):
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.
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
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.
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.