[SOLVED]bool is getting reset by no reason

I have this Turret scrip

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
 
public class Turret : MonoBehaviour {
 
    public Transform target;
    private Enemy targetEnemy;
 
    [Header("General")]
    public float range = 15f;
 
    [Header("Use Bullets (default)")]
    public float fireRate = 1f;
    private float fireCountdown = 0f;
 
    [Header("Use Ice")]
    public bool useIce = false;
    public float slowAmount = .5f;
    public float slowRange = 10;
 
    [Header("Use Fire or Lighting")]
    public bool useFireOrLighting;
 
    public int damageOverTime = 55;
    public float damageRadius;
 
    [Header("Unity Setup Fields")]
    public GameObject rangeView;
    public string enemyTag = "Enemy";
 
    public Transform partToRotate;
    public float turnSpeed = 10f;
 
    public GameObject bulletPrefab;
    public Transform firePoint;
 
    public GameObject weaponTrail;
 
    private Animator anim;
 
    private GameObject thisTurret;
 
    private Vector3 turretBuildPosition;
 
    //[HideInInspector]
    //public GameObject turret;
    [HideInInspector]
    public Node node;
    [HideInInspector]
    public TurretBlueprint turretBlueprint;
    [HideInInspector]
    public bool isUpgradedL2 = false;
    [HideInInspector]
    public bool isUpgradedL3 = false;
 
    BuildManager buildManager;
 
    private void Awake()
    {
        anim = GetComponentInChildren<Animator>();
    }
 
    // Use this for initialization
    void Start ()
    {
        thisTurret = this.gameObject;
 
        InvokeRepeating("UpdateTarget", 0f, 0.5f);
 
        buildManager = BuildManager.instance;
    }
 
    public Vector3 GetBuildPosition()
    {
        return transform.position;
    }
 
    private void OnMouseDown()
    {
        //Debug.Log("This turret is: " + thisTurret.ToString());
        if (EventSystem.current.IsPointerOverGameObject())
        {
            return;
        }
 
        if (thisTurret != null)
        {
            buildManager.SelectTurret(this);
        }
    }
 
    public void UpgradeTurret()
    {
        Debug.Log("L2: " + isUpgradedL2 + " L3: " + isUpgradedL3);
 
        //Upgrade a turret
        if (turretBlueprint !=null && isUpgradedL2 == false && isUpgradedL3 == false)
        {
            if (PlayerStats.Money < turretBlueprint.upgradeCostL2)
            {
                Debug.Log("Not enough money to upgrade that!");
                return;
            }
 
            if (turretBlueprint != null && isUpgradedL2 == false)
            {
                PlayerStats.Money -= turretBlueprint.upgradeCostL2;
 
                //Get rid of the old turret
                Destroy(thisTurret);
 
                //Build a new one
                GameObject _turret = (GameObject)Instantiate(turretBlueprint.upgradePrefabL2, GetBuildPosition(), Quaternion.identity);
                thisTurret = _turret;
                thisTurret.GetComponent<Turret>().turretBlueprint = turretBlueprint;
 
                GameObject effect = (GameObject)Instantiate(buildManager.buildEffect, GetBuildPosition(), Quaternion.identity);
                Destroy(effect, 5f);
               
                thisTurret.transform.GetChild(0).gameObject.SetActive(false);
                thisTurret.transform.GetChild(1).gameObject.SetActive(false);
                BoxCollider bC = thisTurret.AddComponent<BoxCollider>();
                bC.size = new Vector3(2, 34, 2);
 
                buildManager.BoughtTurret();
 
                thisTurret.GetComponent<Turret>().isUpgradedL2 = true;
                Debug.Log("Turret upgraded!");
                Debug.Log("L2: " + isUpgradedL2 + " L3: " + isUpgradedL3);
            }
        }
        else if (turretBlueprint != null && isUpgradedL2 == true && isUpgradedL3 == false)
        {
            if (PlayerStats.Money < turretBlueprint.upgradeCostL3)
            {
                Debug.Log("Not enough money to upgrade that!");
                return;
            }
 
            if (turretBlueprint != null && isUpgradedL3 == false)
            {
                PlayerStats.Money -= turretBlueprint.upgradeCostL3;
 
                //Get rid of the old turret
                Destroy(thisTurret);
 
                //Build a new one
                GameObject _turret = (GameObject)Instantiate(turretBlueprint.upgradePrefabL3, GetBuildPosition(), Quaternion.identity);
                thisTurret = _turret;
                thisTurret.GetComponent<Turret>().turretBlueprint = turretBlueprint;
 
                GameObject effect = (GameObject)Instantiate(buildManager.buildEffect, GetBuildPosition(), Quaternion.identity);
                Destroy(effect, 5f);
 
                thisTurret.transform.GetChild(0).gameObject.SetActive(false);
                thisTurret.transform.GetChild(1).gameObject.SetActive(false);
                BoxCollider bC = thisTurret.AddComponent<BoxCollider>();
                bC.size = new Vector3(2, 34, 2);
 
                buildManager.BoughtTurret();
 
                isUpgradedL3 = true;
                Debug.Log("Turret upgraded!");
                Debug.Log("L2: " + isUpgradedL2 + " L3: " + isUpgradedL3);
            }
        }
        else if (turretBlueprint != null && isUpgradedL2 == true && isUpgradedL3 == true)
        {
            Debug.Log("Max Level!");
        }
    }
 
    void UpdateTarget()
    {
        GameObject[] enemies = GameObject.FindGameObjectsWithTag(enemyTag);
        float shortestDistance = Mathf.Infinity;
        GameObject nearestEnemy = null;
 
        foreach(GameObject enemy in enemies)
        {
            float distanceToEnemy = Vector3.Distance(transform.position, enemy.transform.position);
            if(distanceToEnemy <= range)
            {
                targetEnemy = enemy.GetComponent<Enemy>();
            }
 
            //Default Target
            if (distanceToEnemy < shortestDistance)
            {
                shortestDistance = distanceToEnemy;
                nearestEnemy = enemy;
            }
           
            //Nearest Target
            //if(distanceToEnemy <= range)
            //{
            //    shortestDistance = distanceToEnemy;
            //    nearestEnemy = enemy;
            //}
        }
 
        if(nearestEnemy != null && shortestDistance <= range)
        {
            target = nearestEnemy.transform;
            //targetEnemy = nearestEnemy.GetComponent<Enemy>();
            anim.SetBool("isAttacking", true);
            weaponTrail.SetActive(true);
        }
        else
        {
            target = null;
        }
    }
   
    // Update is called once per frame
    void Update ()
    {
 
        if(target == null)
        {
            anim.SetBool("isAttacking", false);
            weaponTrail.SetActive(false);
            return;
        }
 
        LockOnTarget();
 
        if(fireCountdown <= 0f)
        {
            Shoot();
            fireCountdown = 1f / fireRate;
        }
 
        fireCountdown -= Time.deltaTime;
    }
 
    void LockOnTarget()
    {
        //Target lock on
        Vector3 dir = target.position - transform.position;
        Quaternion lookRotation = Quaternion.LookRotation(dir);
        Vector3 rotation = Quaternion.Lerp(partToRotate.rotation, lookRotation, Time.deltaTime * turnSpeed).eulerAngles;
        partToRotate.rotation = Quaternion.Euler(0f, rotation.y, 0f);
    }
 
    void Shoot()
    {
        //anim.SetBool("isAttacking", true);
        //weaponTrail.SetActive(true);
 
        if(useFireOrLighting)
        {
            FireAndLighting();
        }
        else if(useIce)
        {
            IceAttack();
        }
        else
        {
            //Debug.Log("Use bullets!");
            GameObject bulletGO = (GameObject)Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
            Bullet bullet = bulletGO.GetComponent<Bullet>();
 
            if (bullet != null)
            {
                //Debug.Log("We shoot");
                bullet.Seek(target);
            }
        }
    }
 
    void FireAndLighting()
    {
        //target.GetComponent<Enemy>().TakeDamage(damageOverTime * Time.deltaTime);
 
        Collider[] colliders = Physics.OverlapSphere(transform.position, damageRadius);
 
        foreach (Collider collider in colliders)
        {
            if (collider.tag == "Enemy")
            {
                targetEnemy.TakeDamage(damageOverTime * Time.deltaTime);
            }
        }
    }
 
    void IceAttack()
    {
        //Debug.Log("It use Ice!");
 
        Collider[] colliders = Physics.OverlapSphere(transform.position, slowRange);
       
        foreach (Collider collider in colliders)
        {
            if (collider.tag == "Enemy")
            {
                targetEnemy.Slow(slowAmount);
                GameObject bulletGO = (GameObject)Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
                Bullet bullet = bulletGO.GetComponent<Bullet>();
 
                if (bullet != null)
                {
                    //Debug.Log("We shoot");
                    bullet.Seek(target);
                }
            }
        }
    }
 
 
    private void OnDrawGizmosSelected()
    {
        Gizmos.color = Color.red;
        Gizmos.DrawWireSphere(transform.position, range);
    }
}

The problem is line 94 to line 173. Everything work fine until is upgraded to level 3. The bools are isUpgradedL2 = true and isUpgradedL3 true but when I run the code again they are false and false. I suspect they get reset after I exit the function but I can’t figure out why.

Lines 54 and 56. You intentionally set those to false. Level resets, bools reset. Remove those “= false” and leave unassigned, and be happy

well it did’t work but I have found a solution by adding:

thisTurret.GetComponent<Turret>().isUpgradedL2 = true;
thisTurret.GetComponent<Turret>().isUpgradedL3 = true;

now my Upgrade function look like this:

public void UpgradeTurret()
    {
        //Debug.Log("At the start of the function L2: " + isUpgradedL2 + " L3: " + isUpgradedL3);

        //Upgrade a turret
        if (turretBlueprint != null && isUpgradedL2 == false && isUpgradedL3 == false)
        {
            if (PlayerStats.Money < turretBlueprint.upgradeCostL2)
            {
                Debug.Log("Not enough money to upgrade that!");
                return;
            }

            PlayerStats.Money -= turretBlueprint.upgradeCostL2;

            //Get rid of the old turret
            Destroy(thisTurret);

            //Build a new one
            GameObject _turret = (GameObject)Instantiate(turretBlueprint.upgradePrefabL2, GetBuildPosition(), Quaternion.identity);
            thisTurret = _turret;
            thisTurret.GetComponent<Turret>().turretBlueprint = turretBlueprint;

            //Instantiate effect and Set range and came to inactive
            HideAndCreate();

            thisTurret.GetComponent<Turret>().isUpgradedL2 = true;
            //Debug.Log("Turret upgraded!");
            //Debug.Log("L2: " + isUpgradedL2 + " L3: " + isUpgradedL3);
        }
        else if (turretBlueprint != null && isUpgradedL2 == true && isUpgradedL3 == false)
        {
            if (PlayerStats.Money < turretBlueprint.upgradeCostL3)
            {
                Debug.Log("Not enough money to upgrade that!");
                return;
            }

            PlayerStats.Money -= turretBlueprint.upgradeCostL3;

            //Get rid of the old turret
            Destroy(thisTurret);

            //Build a new one
            GameObject _turret = (GameObject)Instantiate(turretBlueprint.upgradePrefabL3, GetBuildPosition(), Quaternion.identity);
            thisTurret = _turret;
            thisTurret.GetComponent<Turret>().turretBlueprint = turretBlueprint;

            //Instantiate effect and Set range and came to inactive
            HideAndCreate();

            thisTurret.GetComponent<Turret>().isUpgradedL2 = true;
            thisTurret.GetComponent<Turret>().isUpgradedL3 = true;
            isUpgradedL3 = true;
            //Debug.Log("Turret upgraded!");
            //Debug.Log("L2: " + isUpgradedL2 + " L3: " + isUpgradedL3);
        }
        else if (turretBlueprint != null && isUpgradedL2 == true && isUpgradedL3 == true)
        {
            Debug.Log("Max Level!");
        }

        //Debug.Log("Out of the function L2: " + isUpgradedL2 + " L3: " + isUpgradedL3);
    }