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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Variable being changed from false to true before start method?

Discussion in 'Editor & General Support' started by ZTheCdr, Jun 16, 2020.

  1. ZTheCdr

    ZTheCdr

    Joined:
    Apr 25, 2020
    Posts:
    6
    I have the following code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Experimental.GlobalIllumination;
    5.  
    6. public class FireEmitterTiming : MonoBehaviour
    7. {
    8.     public float cycleTime = 4f; //amount of time for the whole cycle to happen (half is spent in off state, half is spent in on state) If cycleTime = 0, always OFF, cycleTime = -1, always ON
    9.     public float startDelay = 0f; //amount of time to delay the start of the cycle
    10.  
    11.  
    12.     [HideInInspector] public bool isOn = false;
    13.     private bool justFlipped = false;
    14.  
    15.  
    16.     private void Start() {
    17.         Debug.Log("isOn: " + isOn); //How is isOn true here?
    18.         //if there is a constant state, there is no need for toggleFire coroutine to be called
    19.         if (cycleTime==0 || cycleTime==-1) {
    20.             StartCoroutine(constantStateWait(cycleTime));
    21.         }
    22.         else {
    23.             StartCoroutine(delayToToggle());
    24.          
    25.         }
    26.     }
    27.     private void Update() {
    28.         var emission = GetComponentInChildren<ParticleSystem>().emission;
    29.      
    30.         if (isOn) { //only if cycleTime==-1, and either the delay is finished or there was no delay (startDelay==0)
    31.             emission.enabled = true;
    32.         }
    33.         else {
    34.             emission.enabled = false;  
    35.         }
    36.      
    37.         if (justFlipped) {
    38.             justFlipped = false;
    39.             StartCoroutine(toggleFire());
    40.         }
    41.     }
    42.  
    43.     private IEnumerator toggleFire() {
    44.         yield return new WaitForSeconds(cycleTime/2);
    45.         isOn = !isOn;
    46.         justFlipped = true;
    47.     }
    48.  
    49.     //once the wait delay is done, then start the  cycle
    50.     private IEnumerator delayToToggle() {
    51.         yield return new WaitForSeconds(startDelay);
    52.         StartCoroutine(toggleFire());
    53.     }
    54.  
    55.     //when the FireEmitter has a constant state (on or off), this is called to wait before setting isOn to true or false
    56.     private IEnumerator constantStateWait(float cycleTime) {
    57.         Debug.Log(startDelay);
    58.         yield return new WaitForSeconds(startDelay);
    59.         Debug.Log("HERE! After the wait!");
    60.         if (cycleTime==0) {
    61.             isOn = false;
    62.         }
    63.         else { //it must be -1 if it is not 0 at this point
    64.          
    65.             isOn = true;
    66.         }
    67.     }
    68. }
    69.  
    The idea is that there are these things that spit out fire, and there can be a delay before they start, or a toggle where they go on for some time, then off, then on, etc. However, that's irrelevant. My question has to do with the bool isOn. I am very clearly instantiating it false at the top, but that Debug.Log() statement at the top of the Start() method keeps telling me that it is true. How could this possibly be?! It starts at false, and Start() is run first, so how in between their is it changing to true?

    isOn is public, but I do not change it from any other script.

    Also, cycleTime is the amount of time it takes for one complete cycle of the flame to happen. If cycleTime = 0 then the flame is always off, and if cycleTime = -1, then the flame is always on.

    Thanks.
     
  2. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    10,004
    HideInInspector does not mean it is not serialized. So whatever is written out in the serialized package that will be loaded. Use [NonSerialized] if you want to not to be able to change it in the editor ever (until you remove this attribute).
    This is the most likely event.
     
  3. ZTheCdr

    ZTheCdr

    Joined:
    Apr 25, 2020
    Posts:
    6
    Alright, thank you. That part of it works now.

    Alright, understood.
     
  4. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    10,004
    Please disregard my previous comment. I was in the wrong place. Your question is completely fine here or in the Scripting. I'm sorry about that.