Recently I’ve had a need to access bools between scripts. My first attempt at this has been successful. My second attempt is failing and I’m having trouble understanding why. What I’m trying to do specifically is determine if a specific bool in another script is true and if it is destroy an obect linked to another. So the 1st script determines the conditions for said bool being true. The 2nd scrit then detectes this and deletes a game object.
This is how I set the bool true in the 1st script:
private bool TorchInRange;
public bool lighting;
private void Start()
{
TorchInRange = false;
lighting = false;
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == "Lightable")
{
TorchInRange = true;
}
}
private void OnTriggerExit2D(Collider2D other)
{
if (other.gameObject.tag == "Lightable")
{
TorchInRange = false;
}
}
private void Update()
{
if (TorchInRange == true)
{
if (Input.GetKeyDown(KeyCode.F))
{
lighting = true;
}
}
}
I think all of this works as intended because when I put debug.log staments in them they all output things. I can also see the bools being enabled and disabled in the inspector.
Here is my 2nd script:
public Torch torch;
public GameObject Wood;
private void OnTriggerEnter2D(Collider2D other)
{
if (torch)
{
if(torch.lighting == true)
{
Destroy(Wood);
}
}
}
private void OnDestroy()
{
torch.lighting = false;
}
I think this is where I’m running into trouble. None of the debug.log statments I plug into the part that refrences the other script show in the console. So where am I going wrong?
Note: Sorry if my code is messy I’m new to this. Also this is my first question that I’ve posted so sorry if its hard to understand.
Edit: Fixed typo