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 Lumart · Apr 29, 2011 at 07:41 PM · collisiongameobjectcollideronmousedownondestroy

OnDestroy collision detect

Hi !

All i want to do is when the function OnDestroy is called i want to know if the object that is colliding with it has the same tag, but if i do like this:

function OnDestroy () {

   if(gameObject.collider.tag == gameObject.tag)
   {
      Destroy(Collision.collider);
   }

}

it's totally wrong right ?

HELP ?

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

4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by sneftel · Apr 29, 2011 at 07:56 PM

OnDestroy doesn't just happen, it is called after you call Object.Destroy. (Or when the scene is unloaded.) Assuming you call Object.Destroy from your OnCollisionEnter, either check the tags then, or save the Collision in a class variable so you'll have it during OnDestroy.

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 Lumart · Apr 29, 2011 at 08:18 PM 0
Share

check bellow please

avatar image
1

Answer by tool55 · Apr 29, 2011 at 07:59 PM

private var isClicked : boolean = false;

   function OnMouseDown()
 {
 isClicked = true;
 }


  function OnCollisionEnter (other : Collision)
     {
     if (other.gameObject.tag == gameObject.tag && isClicked)
     {
     Destroy (other.gameObject);
     Destroy (gameObject);
     }
     }

Once again, haven't tested this, but using a boolean to test for a mouse click may be the simplest way to go.

Comment
Add comment · Show 3 · 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 Lumart · Apr 29, 2011 at 08:18 PM 0
Share

check bellow please

avatar image Lumart · Apr 30, 2011 at 10:23 AM 0
Share

its better thanks !! but still doesnt work because if 2 balls collide and then they separate themselves if i click in one of them, when they get together again they destroy themselves.. i've spent the whole night with it... going to sleep now :D

I'll try again later

Thanks Anyway ;)

avatar image tool55 · May 01, 2011 at 07:04 AM 0
Share

Guess I'm still not clear on what you're trying to achieve. I thought you wanted balls with the same tag to destroy themselves on collision.

avatar image
0

Answer by Lumart · Apr 29, 2011 at 08:08 PM

let me make myself clear:

for example you have 3 blue and 3 red and 3 yellow balls all colliding. What i really want is when i click in a ball it has to check if the collider has the same tag and destroy the two or three colliders.

For example : i click in a blue ball and if there is another blue ball colliding with it they will be destroyed.

All the code that i've got in each ball it's :

function OnMouseDown () {

Destroy(gameObject);

}

function OnDestroy (){

if(gameObject.collider.tag == gameObject.tag) { Destroy(Collision.collider);

}

}

And i'm getting "An instance of type 'UnityEngine.Collision' is required to access non static member 'collider'."

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 sneftel · Apr 29, 2011 at 08:55 PM

In each ball, keep a set of currently intersecting balls with the same tag. Add to this list in OnCollisionEnter and remove from it in OnCollisionExit. In OnMouseDown, delete all balls in that list.

Comment
Add comment · Show 4 · 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 Lumart · Apr 29, 2011 at 09:08 PM 0
Share

sorry but how do you "keep a set of currently intersecting balls" ? they have to be colliding so i can destroy them, for instance, if the 2 balls colide at the begining and after they decolide i cant destroy them

avatar image sneftel · Apr 29, 2011 at 09:47 PM 0
Share

Just like you'd keep around any other information: in a class variable for the behavior. You can use an ArrayList.

avatar image tool55 · Apr 30, 2011 at 01:07 AM 0
Share

I modified my answer above using a boolean to test for the mouse click. Is this what you want?

avatar image sneftel · May 03, 2011 at 05:38 PM 0
Share

$$anonymous$$ind of the opposite. On$$anonymous$$ouseDown is where you want to Do Things. What you need to keep around is not "whether it's clicked right now", but "what balls are intersected right now". The updating of data for use in case of clicks should happen in OnCollisionWhatever; the use of that data for destruction purposes should happen in On$$anonymous$$ouseDown.

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

No one has followed this question yet.

Related Questions

Why do Instantiated GameObjects Colliders only work on player i am controlling,nothing else? 2 Answers

OnCollisionEnter Collision not detected? 1 Answer

Get the script instance associated with a collider 1 Answer

How can I check if an instantiated object collides with another instantiated object? 1 Answer

I need to puck up object using a key, but something is wrong with code 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