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);
}
}
}
}
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
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…
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.