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

Boolean not changing for some reason

Discussion in 'Scripting' started by robert2251, Mar 1, 2020.

  1. robert2251

    robert2251

    Joined:
    Nov 27, 2018
    Posts:
    15
    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?
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class EnemyLaserMovement : MonoBehaviour
    6. {
    7.     public float velY = 5f;
    8.     float velX = 0f;
    9.     Rigidbody2D rb;
    10.     private Health health;
    11.     public bool doDamage = false;
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         health = GameObject.FindObjectOfType<Health>();
    16.         rb = GetComponent<Rigidbody2D>();
    17.     }
    18.  
    19. // Update is called once per frame
    20. void Update()
    21.     {
    22.         rb.velocity = new Vector2(velX, velY);
    23.      
    24.     }
    25.  
    26.     void OnTriggerEnter2D(Collider2D collider)
    27.     {
    28.         if (collider.gameObject.tag == "shredder")
    29.         {
    30.             Destroy(gameObject);
    31.  
    32.         }
    33.         if (collider.gameObject.tag == "playerTag")
    34.         {
    35.             if (doDamage == true)
    36.             {
    37.                 health.health--;
    38.                 Destroy(gameObject);
    39.             }
    40.             else
    41.             {
    42.                 Destroy(gameObject);
    43.             }
    44.         }
    45.     }
    46. }
    47.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    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.
     
  3. robert2251

    robert2251

    Joined:
    Nov 27, 2018
    Posts:
    15
    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
     
  4. adi7b9

    adi7b9

    Joined:
    Feb 22, 2015
    Posts:
    181
    https://docs.unity3d.com/Manual/Coroutines.html
    Code (CSharp):
    1. void OnTriggerEnter2D(Collider2D collider)
    2. {
    3.   doDamage = false;
    4.   StartCouroutine(Wait30Sec);
    5. }
    6.  
    7. IEnumerator Wait30Sec()
    8. {
    9.   yield return new WaitForSeconds(30f);
    10.   doDamage = true;
    11. }
     
  5. robert2251

    robert2251

    Joined:
    Nov 27, 2018
    Posts:
    15
    I tried this yesterday, it still doesn't change though.
     
  6. adi7b9

    adi7b9

    Joined:
    Feb 22, 2015
    Posts:
    181
    Then when you're instantiate your prefab object.. just do
    Code (CSharp):
    1. go.GetComponent<YourScript>().doDamage = false or true;
    2. //YourScript >> is the name of your script
    3. //go >> it's your instantiated object
     
  7. robert2251

    robert2251

    Joined:
    Nov 27, 2018
    Posts:
    15
    Idk bro I cant understand you, even the OR should be || in c#. Something isn't working with your code
     
  8. adi7b9

    adi7b9

    Joined:
    Feb 22, 2015
    Posts:
    181
    Code (CSharp):
    1. go.GetComponent<YourScript>().doDamage = false;
    2. //or you can use
    3. go.GetComponent<YourScript>().doDamage = true;
    You wanted to know how to change your boolean. Title: "Boolean not changing for some reason".
     
  9. robert2251

    robert2251

    Joined:
    Nov 27, 2018
    Posts:
    15
    I'll try again. Thanks
     
  10. VSMGames

    VSMGames

    Joined:
    Jan 12, 2020
    Posts:
    47
    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.
     
  11. robert2251

    robert2251

    Joined:
    Nov 27, 2018
    Posts:
    15
    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..
     
  12. robert2251

    robert2251

    Joined:
    Nov 27, 2018
    Posts:
    15
    Code (CSharp):
    1.  public EnemyLaserMovement laser;
    2.  
    3.     // Start is called before the first frame update
    4.    /* void Start()
    5.     {
    6.  
    7.         //laser = GameObject.FindObjectOfType<EnemyLaserMovement>();
    8.     } */
    9.  
    10.     void Awake()
    11.     {
    12.  
    13.         GameObject star = GameObject.FindWithTag("enemyLaser");
    14.         laser = star.GetComponent<EnemyLaserMovement>();
    15.     }
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.     }
    20.  
    21.     void OnTriggerEnter2D(Collider2D collider)
    22.     {
    23.  
    24.         if (collider.gameObject.tag == "playerTag")
    25.         {
    26.             laser.GetComponent<EnemyLaserMovement>().doDamage = false;
    27.             //laser.doDamage = false;
    28.             Destroy(gameObject);
    29.             }
    30.          
    31.         }
    32.      
    33.     }
    34.