Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

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

Discussion in 'Scripting' started by Dj_Diamond3, Apr 27, 2024.

  1. Dj_Diamond3

    Dj_Diamond3

    Joined:
    Feb 28, 2023
    Posts:
    5
    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:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. [System.Serializable]
    4. public class Cooldown
    5. {
    6.     [SerializeField] private float cooldownTime;
    7.     float _nextFireTime;
    8.  
    9.     public bool IsCoolingDown => Time.time < _nextFireTime;
    10.     public void StartCooldown() => _nextFireTime = Time.time + cooldownTime;
    11. }
    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):

    Code (CSharp):
    1. void OnShoot(InputValue value)
    2.     {
    3.         shootPressed = value.isPressed;
    4.  
    5.         if (shootCooldown.IsCoolingDown) return;
    6.  
    7.         Vector3 dir = new Vector3(transform.forward.x, 0, transform.forward.z).normalized;
    8.         GameObject bullet = Instantiate(bulletPrefab, shootOrig.position, Quaternion.identity);
    9.         BulletController bulletController = bullet.GetComponent<BulletController>();
    10.         bulletController.target = shootOrig.position + dir * bulletHitMiss;
    11.         bulletController.hit = false;
    12.         bulletController.bulletSpeed = bulletSpeed;
    13.  
    14.         shootCooldown.StartCooldown();
    15.  
    16.     }
    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
     
  2. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,936
    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.
     
  3. Dj_Diamond3

    Dj_Diamond3

    Joined:
    Feb 28, 2023
    Posts:
    5
    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
     
  4. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,936
    Verify the cooldownTime is set in the prefabs, that it's not 0.
     
  5. Dj_Diamond3

    Dj_Diamond3

    Joined:
    Feb 28, 2023
    Posts:
    5
    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
     
  6. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,687
    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.

    Code (csharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Test : MonoBehaviour
    6. {
    7.     public Cooldown cooldown;
    8.  
    9.     private void Update()
    10.     {
    11.         if (cooldown.IsCoolingDown) return;
    12.  
    13.         Debug.Log("Foo.");
    14.  
    15.         cooldown.StartCooldown();
    16.     }
    17. }
     
  7. Dj_Diamond3

    Dj_Diamond3

    Joined:
    Feb 28, 2023
    Posts:
    5
    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.
     
  8. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,687
    I tried that by deleting it from the scene and recreating it. I didn't try deleting the scene and creating a new one.
     
  9. Dj_Diamond3

    Dj_Diamond3

    Joined:
    Feb 28, 2023
    Posts:
    5
    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.