Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Private bool changing of its own accord!

Discussion in 'Scripting' started by Flynn_Prime, Aug 19, 2018.

  1. Flynn_Prime

    Flynn_Prime

    Joined:
    Apr 26, 2017
    Posts:
    383
    This has really been battering my head and, as always, I imagine it will be some stupid mistake. But I can not for the life of me figure out the behaviour of this code. I'm using a system called Mixins to create weapon/spell behaviours and the following code is just for a cooldown/fire rate effect. As you can see, I set isReady = true when I declare the variable. However, for some reason the bool returns false instantly, and never gets set true again! (I put a breakpoint just under the if statement in the Update function, and it gets hit as soon as the script gets instantiated, meaning that within) Except, I can see via the inspector that the bool remains true the whole time. I really hope someone can explain this.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Cooldown : MixinBase
    4. {
    5.     public string cooldownDescription;
    6.     public float cooldownTimer;
    7.     private float cooldownTime;
    8.     [SerializeField] private bool isReady = true;
    9.  
    10.     public override bool Check()
    11.     {
    12.         return isReady;
    13.     }
    14.  
    15.     public override void Action()
    16.     {
    17.         isReady = false;
    18.         cooldownTime = 0.0f;
    19.     }
    20.  
    21.     private void Update()
    22.     {
    23.         if (!isReady)
    24.         {
    25.             cooldownTime += Time.deltaTime;
    26.             if (cooldownTime >= cooldownTimer)
    27.             {
    28.                 isReady = true;
    29.             }
    30.         }
    31.     }
    32. }
    P.S I also used this exact same script in another project with no problems whatsoever (checked said project after updating Unity and it still functions correctly).
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Serialized fields are reset to whatever the value in the inspector is set to when you save the object / prefab. Your initializing value there is only accurate when you first add the script to an object.

    If, for some reason, you can't set the value to true in the inspector before running the game, you can override the value in Awake().
     
    Kiwasi likes this.
  3. Flynn_Prime

    Flynn_Prime

    Joined:
    Apr 26, 2017
    Posts:
    383
    I tried to set the value via the Awake function, however had the same issue.

    I have fixed it since though. I had a slight reference error when I assigned my skills to their skill buttons (assigned the prefab and not a clone of the prefab). Thanks for the help.