- Home /
How can I access a bool in another script to destroy a game object?
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
I think you forgot to put the uppercase D in 2D in the second script like this:
OnTriggerEnter2D
Answer by outramjamie_unity · Oct 29, 2020 at 02:12 PM
small typo, you've defined a private method called OnTriggerEnter2d(Collider2D other) rather than the event function OnTriggerEnter2D(Collider2D other) so the method is never called, should be:
private void OnTriggerEnter2D(Collider2D other)
{
if (torch)
{
if (torch.lighting == true)
{
Destroy(Wood);
}
}
}
I fixed the typo but the code still doesnt do anything.
I think the issue is that OnTriggerEnter2D is only called when the torch first touches the wood. so you are only checking if F is pressed down the same frame that F can be pressed down to destroy the wood which is near impossible to do, you want to check if F is pressed whilst the colliders are overlapping so use OnTriggerStay2D(Collider2D other) which will be called every frame whilst the colliders are overlapping.
private void OnTriggerEnter2D(Collider2D other)
{
Debug.Log("Collision enter Called Once Only");
}
private void OnTriggerStay2D(Collider2D other)
{
Debug.Log("Collision Stay Called");
if (torch)
{
if (torch.lighting == true)
{
Destroy(Wood);
}
}
}
Answer by Spip5 · Oct 29, 2020 at 11:33 AM
This part of your code does not make much sense :
private void OnTriggerEnter2d(Collider2D other)
{
if (torch)
{
if(torch.lighting == true)
{
Destroy(Wood);
}
}
}
You need to check if the collider is what you are tracking (I am assuming your first script is the Torch class)
if(other.tag == "torch")
{
if(other.GetComponent<Torch>().isLightning)
{
Destroy(Wood);
}
}
}
This hasn't worked for me. Sense that part of the code doesn't make much sense I'll try to expalian myself a bit there. I'm trying to access the bool from the other script and check if its true. I've doen somthing similar in a diffrent script and had it work. This one doesn't. I thought it might be because the script is having trouble checking the other. Also, the torch doesnt have the tag "torch" because I asigned it a diffrent one. The tag I assigned it will be applied to other objects that wont be able to light things on fire hence why I havent used it yet. I figuered that instead I could assign the wood pile the tag "Lightable" to get around it. Here I'll post a snippet of this other code I'm talking about, the one that works. 1st script:
public bool LeftHandFull;
private void Start()
{
LeftHandFull = false;
}
private void OnTriggerEnter2D(Collider2D left)
{
if (left.gameObject.tag == "PickedUpItem")
{
LeftHandFull = true;
Debug.Log("Picked");
}
}
2nd Script:
public LeftHand lefthand;
private void OnTriggerEnter2D(Collider2D collision)
{
Anim.SetBool("IsTriggered", true);
if (lefthand)
{
if (lefthand.LeftHandFull == false)
{
Press$$anonymous$$gameObject.SetActive(true);
OnTrig = true;
}
}
}
I don't know if I've clarified myself at all. If your still not sure about it could you let me know what part needs to be ellaborated on? Thank you.
In this case, it sounds like an implementation problem. If it works on a script and not another then maybe you are doing something wrong in the editor, have you tried Debugging everything to check what is blocking you and where?
The breakpoints I set up have been stopping me in the right places. The place they havent stoped me is in the Destroy(Wood) part of the code. I dont really get why that is the case. As far as I know everything else is working fine in the editor. I can see the bools geting checked off in the inspector as I go through the process.
Answer by thekrocker · Aug 13, 2021 at 11:37 PM
Hey, are you be able to fix that? I am having the same issue.