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
0
Question by darkmwar · Jul 22, 2014 at 03:10 AM · 2dbuttontriggerresetpress

How to configure Gui Buttons and trigger2D correctly?

This is what I want to achieve:

When the button is pressed AND the object is in the "trigger zone" then destroy the object and reset the button.

If the button is pressed AND object not in the zone, reset the button (unpress it).

The problem is that the button doesn't get reset when the object is not in the zone, and i can't think of another way :(

 void OnTriggerStay2D (Collider2D other)
 {
     if (leftClick) {
              leftClick = false;
              if(other.gameObject.name.Contains("abc"){
              Destroy (other.gameObject); }
     }
  }                        
  private void OnGUI ()
  {
     Rect aRect = new Rect (0,0,20,20);
     if (GUI.Button (aRect, "") || Input.GetKeyDown ("left")) {
                             leftClick = true;
                     }
     }

The problem here is that leftClick = false; (to reset button), doesn't get executed if there is nothing in the trigger zone, so if I press the button before the object enters, it doesn't get reset.

I want the button to reset to false (unpress) when the object is not in the trigger zone

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 darkmwar · Jul 23, 2014 at 06:45 PM 0
Share

Hints anyone who dealed with similar issues?

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by robertbu · Jul 23, 2014 at 07:19 PM

Sometimes things like this are cumbersome in Unity. I can think of two approaches. If you will only have one collision with the specific type of object at the same time you can do something like this:

 void OnTriggerEnter2D (Collider2D other)
 {
 
     if(other.gameObject.name.Contains("abc"){
           goInside = other.gameObject; 
      }
 }
 
 void OnTriggerExit2D (Collider2D other)
 {
 
     if(other.gameObject.name.Contains("abc"){
           goInside = null; 
      }
 }

Then in OnGUI() inside your button/key check if() you can check for a non-null value of goInside and do the Destroy() there. The other solution is to clear the liftClick flag after each frame. According to this diagram, you should be able to do it at the end of the frame (untested by me). So you could leave your OnTriggerStay2D() and then rewrite your OnGUI() code as follows:

 private void OnGUI ()
  {
     Rect aRect = new Rect (0,0,20,20);
     if (GUI.Button (aRect, "") || Input.GetKeyDown ("left")) {
         leftClick = true;
         StartCoroutine(ClearStay());
     }
 }
 
 IEnumerator ClearStay() {
     yield return new WaitForEndOfFrame();
     leftClick = false;
 }

Depending on the order of things, you might have to yield twice:

 IEnumerator ClearStay() {
     yield return new WaitForEndOfFrame();
     yield return new WaitForEndOfFrame();
     leftClick = false;
 }


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

2 People are following this question.

avatar image avatar image

Related Questions

Changing the sliding door script in Angry Bots to open at a button press. 0 Answers

Get information on ignored collision 1 Answer

Button animation (transition) reset 0 Answers

How to detect button presses and change child object text in the button's Parent Object script 0 Answers

[2020.3][2D Collision] Trigger collider with physics computation 0 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