Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Dommnk · Apr 18 at 09:26 PM · unity 2deventsystem

How to ignore some events occurrence

Hi, Let's Imagine player shoots gameObjects with different tags. Let that be "Square", "Hex", and "Rectangle". When player shoots gameObject with tag "Square" and hits another gameObject with the same tag "Square", from previous shoots, I want to destroy previous gameObject and have the one which has been recently shot remain. Problem is when gameObject hits the another gameObject with the same tag, an event is fired twice and this makes both gameObjects disappear. Code I am using allows only one occurrence, so one GameObject will in fact get destroyed, but when player hits more than one GameObject with the same tag "Square", those will remain, and I want them to be destroyed as well, so in the end only remains the one that was shot lastly, and all gameObjects with the same tag "Square" that were hit in that particular shot get destroyed. Do you have any suggestions how can I do this?

  private void OnCollisionEnter2D(Collision2D collision)
     {
         if (ignoreCollision) return;
 
         Invoke("Landed", 1f); // creates new gameObject for the player to shoot
         Debug.Log(gameObject.tag + " collided with " + collision.gameObject.tag);
         ignoreCollision = true;
 
         if (collision.gameObject.tag == gameObject.tag)
         {
             Destroy(collision.gameObject);
         }
     }
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

2 Replies

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

Answer by Dommnk · Apr 19 at 06:07 PM

Okay I managed to solve this issue myself using simple public bool isDestructible. Writing it down if anyone stumbles into same problem. So by default prefabs are not destructible - isDestructible equals to false. At the end of the script I change this property to true. So for next move it will be destroyable, however the one thats being shot is not. Works like a charm.

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 ogulcan_topsakal · Apr 18 at 10:55 PM

@Dommnk You can use the OnTriggerEnter2D event inside the objects you target instead of the gameobject you fired. When comparing tags, I recommend you to use them as in the example I gave Simple explanation.


target square, hex, triangle etc.

 private void OnCollisionEnter2D(Collision2D col)
 {
     if (col.gameObject.CompareTag(gameObject.tag))
     {
         Destroy(gameObject);
     }
 }



Comment
Add comment · Show 5 · 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 Dommnk · Apr 19 at 01:32 PM 0
Share

@$$anonymous$$ Thank you for response. This condition however makes both objects getting destroyed - both gameObject and col.gameObject, and this breaks my code - not executing Landed method. Invoke("Landed"); gets canceled.

avatar image ogulcan_topsakal Dommnk · Apr 19 at 01:35 PM 0
Share

@Dommnk Remove destroy part on fired objects move that part to targets as given an example. If fired and target is same prefab I would separate that prefabs.

avatar image Dommnk ogulcan_topsakal · Apr 19 at 03:35 PM 0
Share

@$$anonymous$$ Sorry I don't understand what you mean "move to targets" as I handle every event in one class - BoxScipt. Exactly it is that fired object and target are the same prefabs, do you think that creating two sets of prefabs - one for the player to shoot and one for those existing ones in game would solve issue?

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

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

Related Questions

OnDrag EventSystem not working on iPhone device,OnDrag not working on device 0 Answers

Event trigger i put on an empty game Object isn't responding even though i have an Event System and everything ,Unity Event trigger isn't responding to clicks 1 Answer

Problem with Main Menu 1 Answer

Not getting normal ads or test ads using admob 0 Answers

Cinemachine glitch 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