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 youngapprentice · Apr 15, 2013 at 12:42 AM · triggerempty

How to check if Trigger is 'empty'?

Hi, all!

I have a trigger that turns the cursor green OnTriggerEnter, and red OnTriggerExit. However, sometimes that makes it turn red even when things are still inside of it.

I would like it to turn red only when it is completely empty, and turn green the moment anything is inside of it.

it would be along the lines of:

 If( triggerIsEmpty){
     GREEN!
 }
 else{
     RED!
 }

Anyone have some real code for my pseudo code? I could only find Enter, Exit, and Stay, and I don't think that that will work out for me.

Thanks!- YA

Comment
Add comment
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

6 Replies

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

Answer by Chronos-L · Apr 15, 2013 at 12:55 AM

This might be a bit of a overkill.

How about this:

 public class SlightAdvanceTrigger : MonoBehaviour {
 
    private int _count;
 
    void Start() {
       _count = 0;
      
      //You can setup the trigger here in script, or in the editor, doesn't really matter
    }
 
    void OnTriggerEnter( Collider other ) {
       ++_count;
    }
 
    void OnTriggerExit( Collider other ) {
       --_count;
    }
 
    bool isEmpty() {
       return _count == 0;
    }
 }

Please note that I do not know whether if the OnTriggerExit() will be called when a gameobject is destroyed in the trigger. If you have that sort of action, you will need to keep track of the gameobject using a List< T >, and reset the _count accordingly everytime you need to access the _count or isEmpty().

Comment
Add comment · Show 9 · 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 Unitraxx · Apr 15, 2013 at 01:05 AM 0
Share

Ha we came up with the same solution. I believe a solution with only OnTriggerStay() could be made too.

avatar image Chronos-L · Apr 15, 2013 at 01:24 AM 0
Share

How to do it using just OnTriggerStay()? Do we need a collection to check whether if a certain gameObject in the trigger is already counted? Or is it some other way that I did not think of?

avatar image youngapprentice · Apr 15, 2013 at 01:40 AM 0
Share

This is how I thought it would go. I was hoping for a magic function but it's not like this is hard or anything. Thanks, guys! - On a side note, I would be interested on how you could use OnTriggerStay for this.

Would it be close to:

     function OnTriggerStay (other : Collider) {
         if (other)
             GREEN!
         else RED!
     }


avatar image youngapprentice · Apr 15, 2013 at 09:49 PM 0
Share

Well, shoot. It doesn't detect when enemies die -_- How would I go about this, now?

avatar image Chronos-L · Apr 16, 2013 at 01:12 AM 0
Share

There are 2 ways to approach this:

Trigger Notification

Notify.cs

 ...
 using System.Collections.Generic;
 
 public class Notify : $$anonymous$$onoBehavior {
    public List<SlightAdvanceTrigger> advTriggers;

    //Can be called by Send$$anonymous$$essage() when the enemy dies
    public void Die() {
       foreach( SlightAdvanceTrigger t in advTriggers ) {
          t.Decrease();
       }
    }
 }

SlightAdvanceTrigger.cs

 public class SlightAdvanceTrigger : $$anonymous$$onoBehaviour {
  
    private int _count;
  
    void Start() {
       _count = 0;
    }
  
    void OnTriggerEnter( Collider other ) {
       ++_count;
       Notify n = other.gameObject.GetComponent<Notify>();
 
       if( n == null ) {
          n = other.gameObject.AddComponent<Notify>();
          n.advTriggers = new List<SlightAdvanceTrigger>();
       }
       n.advTriggers.Add( this );
    }
  
    void OnTriggerExit( Collider other ) {
       --_count;
       n.AdvTriggers.Remove( this );
    }
  
    bool isEmpty() {
       return _count == 0;
    }
 
    public void Decrease() {
       --_count;
    }
 }
Show more comments
avatar image
11

Answer by OnlyJoe · Jan 08, 2014 at 04:31 AM

I know this is a bit old now. But incase anyone finds it in search.

The most simple way to do this is using the OnTriggerStay, and setting a bool to true when the frame starts, and then false if there are any active collisions. I really don't know why unity doesn't give you access to a list of all active collisions that a collider has, then you could just check the count on a collider.

 public class SlightAdvanceTrigger : MonoBehaviour {
 
     public bool isEmpty;
 
     void FixedUpdate()
     {
         isEmpty = true;
     }
 
     void OnTriggerStay(Collider other)
     {
         isEmpty = false;
     }
 
 }
Comment
Add comment · Show 1 · 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 youngapprentice · Jan 08, 2014 at 04:39 AM 0
Share

Thanks for the answer, man! This is old, and I am pretty sure I ended up going with the counting method. Hopefully this will help anyone else that stumbles over here. thanks again!

avatar image
1

Answer by Unitraxx · Apr 15, 2013 at 01:03 AM

You could keep track with a counter of how many objects are in the trigger zone.

 private int counter = 0;

 void OnTriggerEnter(Collider other) {
     counter++;
 }

 void OnTriggerExit(Collider other) {
     counter--;
 }

and then your pseudocode, which is probably placed in Update(), can be changed to :

 if (counter == 0){
     GREEN!
 }else{
     RED!
 }

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
avatar image
0

Answer by SasugaShogun · Feb 04, 2014 at 06:51 AM

Is there any way to check if a collider is empty without using the OnTrigger functions?

Comment
Add comment · Show 1 · 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 FullHeartGames · Mar 05, 2016 at 01:05 PM 0
Share

I wouldn't think so that seems to be the only way to detect colliders..?

avatar image
0

Answer by robertseegrist · Feb 26, 2018 at 06:12 PM

An alternative to using Triggers:

 public Renderer myRenderer;
 public Transform checkOrigin;
 public float checkRadius;
 public LayerMask checkMask;
 
 void Update()
 {
 Collider[] checkResults = new Collider[1];
 //OverlapSphereNonAlloc does not create garbage
 int collidersHit = Physics.OverlapSphereNonAlloc(checkOrigin.position, checkRadius, checkResults, QueryTriggerInteraction.Ignore);
 if(collidersHit > 0) {
 myRenderer.material.color = Color.green;
 }
 else
 {
 myRenderer.material.color = Color.red;
 }
 }



Comment
Add comment · Show 1 · 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
  • 1
  • 2
  • ›

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

20 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

Related Questions

Attack Animation Trigger 1 Answer

Can't click gameobject when over another trigger? 1 Answer

Collision/Trigger with Playmaker 0 Answers

How to play a song when a key is pressed down within a trigger? Trying to make a Radio :P 2 Answers

How do I trigger an animation using a button? 3 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