Boolean not changing for some reason

Hi all, as you can see the bool doDamage is set to false, although when clickin on the prefab with the script attached the box is ticked (meaning it’s true). Why isn’t it changing to false?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyLaserMovement : MonoBehaviour
{
    public float velY = 5f;
    float velX = 0f;
    Rigidbody2D rb;
    private Health health;
    public bool doDamage = false;
    // Start is called before the first frame update
    void Start()
    {
        health = GameObject.FindObjectOfType<Health>();
        rb = GetComponent<Rigidbody2D>();
    }

// Update is called once per frame
void Update()
    {
        rb.velocity = new Vector2(velX, velY);
      
    }

    void OnTriggerEnter2D(Collider2D collider)
    {
        if (collider.gameObject.tag == "shredder")
        {
            Destroy(gameObject);

        }
        if (collider.gameObject.tag == "playerTag")
        {
            if (doDamage == true)
            {
                health.health--;
                Destroy(gameObject);
            }
            else
            {
                Destroy(gameObject);
            }
        }
    }
}

Unity serializes the true value “back in” from your prefab, which is an asset on disk.

The false you set above only happens the very first time you add the script to a GameObject.

After that, Unity loads it from the prefab asset instance.

1 Like

Thanks for the answer, how can I overcome this? I want a collider to disable the boolean on entry, wait 30 secs and then re-enable it. Is it possible? Thanks

void OnTriggerEnter2D(Collider2D collider)
{
  doDamage = false;
  StartCouroutine(Wait30Sec);
}

IEnumerator Wait30Sec()
{
  yield return new WaitForSeconds(30f);
  doDamage = true;
}

I tried this yesterday, it still doesn’t change though.

Then when you’re instantiate your prefab object… just do

go.GetComponent<YourScript>().doDamage = false or true;
//YourScript >> is the name of your script
//go >> it's your instantiated object

Idk bro I cant understand you, even the OR should be || in c#. Something isn’t working with your code

go.GetComponent<YourScript>().doDamage = false;
//or you can use
go.GetComponent<YourScript>().doDamage = true;

You wanted to know how to change your boolean. Title: “Boolean not changing for some reason”.

I’ll try again. Thanks

Try to be more specific of what are you trying to achieve. Like Kurt-Dekker wrote when assigned a boolean you have set it’s default value to false, but since it is a public variable it is visible/changeable in your prefab’s inspector window, where you have ticked the box which basically overrides the default false value and makes it true. If you want it to be false, untick the box. If you want it to be changed into true in 30 seconds after colliding with something then adi7b9 provided an example for that.

Thanks for your reply. Basically I have 2 scripts. One script attached to the prefab has this doDamage boolean. Now I have another script, I am trying to change the boolean from one script to another. I tried many things but for some reason it doesn’t change. I’m trying to follow adi7b9, I think im not managing to instantiate the gameobject properly…

 public EnemyLaserMovement laser;

    // Start is called before the first frame update
   /* void Start()
    {

        //laser = GameObject.FindObjectOfType<EnemyLaserMovement>();
    } */

    void Awake()
    {

        GameObject star = GameObject.FindWithTag("enemyLaser");
        laser = star.GetComponent<EnemyLaserMovement>();
    }
    // Update is called once per frame
    void Update()
    {
    }

    void OnTriggerEnter2D(Collider2D collider)
    {

        if (collider.gameObject.tag == "playerTag")
        {
            laser.GetComponent<EnemyLaserMovement>().doDamage = false;
            //laser.doDamage = false;
            Destroy(gameObject);
            }
          
        }
     
    }

same happening here

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FirePistol : MonoBehaviour
{

    public GameObject TheGun;
    public GameObject MuzzleFlash;
    public AudioSource GunFire;
    public bool IsFiring = false;

    void Update()
    {
        if (Input.GetButtonDown("Fire1"))
        {
            if (IsFiring == false)
            {
                StartCoroutine(FiringPistol());
            }
        }

    }

    IEnumerator FiringPistol()
    {
        IsFiring = true;
        TheGun.GetComponent<Animation>().Play("PistolShot");
        MuzzleFlash.SetActive(true);
        MuzzleFlash.GetComponent<Animation>().Play("MuzzleAnim");
        GunFire.Play();
        yield return new WaitForSeconds(0.5f);
        MuzzleFlash.SetActive(false);
        IsFiring = false;
    }
}

Since you don’t want your bool to be edited from the Editor, just don’t make it Non Serializable. Either make it private or add [System.NonSerialized] to it.

1 Like

I think you meant the other way around. Make it NonSerializable or don’t make it serializable.

Please do not necro old thread. Open your own next time.

1 Like