Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by perendl · Mar 16, 2016 at 11:54 AM · collider2dtimerontriggerstaycomparetag

Delete objects in CircleCollider2D (OnTriggerStay2D)

Hello there! I want want to delete all objects with a specific tag all around one object with this script, but it just deletes one of them all 10 seconds, how can i do it that it deletes all in the CircleCollider2D (☑ IsTiggered) ?

 public class _Destroy_Script_ : MonoBehaviour
 {
     public float Destroy_Timer = 10f;
 
     void Start()
     {
         Destroy_Timer = 10f;
     }
 
     void OnTriggerStay2D(Collider2D other)
     {
         Destroy_Timer -= Time.deltaTime;
 
         if (Destroy_Timer <= 0)
         {
 
             if (other.gameObject.CompareTag("Destroy_Tag"))
             {
                 Destroy(other.gameObject);
             }
             Destroy_Timer = 10f;
         }
 
     }
 
 }
 

Ty for reading and coming answers, excuse my bad english.

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

1 Reply

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

Answer by See_Sharp · Mar 16, 2016 at 12:21 PM

You'll need a specific timer for each new object entered.

You could create a dictionary (import System.Collections.Generic) and map a time with a gameobject. Something like this:

     Dictionary<GameObject, float> mappedTimers = new Dictionary<GameObject, float>();
     public float Destroy_Timer = 10f;
     
     void OnTriggerStay2D(Collider2D other) {
         //add to dictionary if it has the destroy tag and if it's no in there
         if (other.gameObject.CompareTag("Destroy_Tag") && !mappedTimers.ContainsKey(other.gameObject)) {
             //add with a timer value of 10
             mappedTimers.Add(other.gameObject, Destroy_Timer);
         }
         //now if we have this gameobect, substract time
         if(mappedTimers.ContainsKey(other.gameObject)) {
             mappedTimers[other.gameObject] -= Time.deltaTime;
             //check after substracting
             if (mappedTimers[other.gameObject] <= 0) {
                 //remove from dictionary, because euhm, we're about to destroy it i guess ;)
                 mappedTimers.Remove(other.gameObject);
                 Destroy(other.gameObject);
             }
         }
     }


Didn't compile the code tho. But my instinct tells me this seems ok :)

Comment
Add comment · Show 6 · 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 perendl · Mar 16, 2016 at 01:21 PM 0
Share

It worked fine! Im pretty new to c# and unity, have just worked with .net and delphi, have never worked with dictionairies before, great to know! :D i also added a dic. with alpha-value so the objekts become more and more transpary. ty :-)

avatar image See_Sharp perendl · Mar 16, 2016 at 01:29 PM 0
Share

Awesome :D Yes, Generics are super sweet! Can't live without them ;)

avatar image perendl See_Sharp · Mar 18, 2016 at 09:41 AM 0
Share

Hey there! I need this script u gave me now for all objects with a specific tag but not in range of an collider, how can i ask for all objects with one tag? if (gameObject.CompareTag("Spawn_Gold_Tag") ?

Show more comments
Show more comments

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

51 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

Related Questions

CompareTag vs gameObject for best perfmormance? 0 Answers

How to write an overlap sphere 2D 0 Answers

Trying to use OnTriggerStay to continuously decrease my health bar but only triggers once. How to fix this? 0 Answers

How to remove/freeze timer on Destroy() 2 Answers

How to suspend an action? 1 Answer


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