Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by colehadlock · Oct 29, 2020 at 12:58 AM · bool

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Anis1808 · Oct 29, 2020 at 01:07 AM 0
Share

I think you forgot to put the uppercase D in 2D in the second script like this:

 OnTriggerEnter2D

3 Replies

· Add your reply
  • Sort: 
avatar image
1

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image colehadlock · Oct 29, 2020 at 02:28 PM 0
Share

I fixed the typo but the code still doesnt do anything.

avatar image outramjamie_unity colehadlock · Oct 29, 2020 at 04:15 PM 0
Share

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

avatar image
0

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


Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image colehadlock · Oct 29, 2020 at 01:25 PM 0
Share

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.

avatar image Anis1808 colehadlock · Oct 29, 2020 at 02:40 PM 0
Share

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?

avatar image colehadlock Anis1808 · Oct 29, 2020 at 02:56 PM 0
Share

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.

Show more comments
avatar image
0

Answer by thekrocker · Aug 13, 2021 at 11:37 PM

Hey, are you be able to fix that? I am having the same issue.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

140 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Need figuring out how to return values of original rect size using toggle buttons.Floorplan rec stretches out when RoomViews button toggled open but doesn't collaspe. Have tried researching but no luck. 0 Answers

Error: Cannot implicitly convert type `float' to `bool' 1 Answer

Problem with scripts ignoring my variables/functions? 0 Answers

Add a bool to animator & activate by key press 1 Answer

OnTriggerEnter if else 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges