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 cidmodder · Feb 09, 2011 at 08:39 PM · javascriptcollision

two objects colliding

I have two of the same prefabs colliding and when they hit I want them both to delete. I can only ever get one to delete and not the other. how do I do that?

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

3 Replies

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

Answer by StephanK · Feb 15, 2011 at 02:23 PM

add a script to the prefab that does this:

function OnTriggerEnter(other : Collider) {
  var destructable = other.GetComponent("Destructable");
  if (destructable != null)
    GameObject.Destroy(gameObject);
}

and is named Destructable.js. Now make sure your objects have a collider that is set to trigger and a rigidbody, otherwise there will be no collision/trigger at all.

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
4

Answer by e-bonneville · Feb 15, 2011 at 05:39 PM

In C#

void OnCollisionEnter(Collision collision) {
    Destroy(collision.collider.gameObject);
    Destroy(gameObject);
}

In Javascript

function OnCollisionEnter(collision : Collision) {
    Destroy(collision.collider.gameObject);
    Destroy(gameObject);
}
Comment
Add comment · Show 10 · 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 Demigiant · Feb 19, 2011 at 02:24 PM 0
Share

If it's 2 instances of the same prefab, Destroy(gameObject) should be enough when used in OnCollisionEnter, cause it would be called on both.

avatar image e-bonneville · Feb 19, 2011 at 10:12 PM 0
Share

The very term instance indicates that it's separated from the original prefab in some way. If I call delete on one of the gameObjects, it will delete only that instance of it.

avatar image Demigiant · Feb 20, 2011 at 10:42 AM 0
Share

Yep, but both instances share the same prefab's methods, thus if the prefab contains a "Destroy self" method which is automatically called on collision detection, they should both call it and be destroyed.

avatar image e-bonneville · Feb 20, 2011 at 02:06 PM 0
Share

Well, I must say, I've done a lot of work with prefab instances, and that is simply not the case. It does seem logical for both of them to be destroyed, but that's not what happens.

avatar image Demigiant · Feb 20, 2011 at 03:36 PM 0
Share

Hey Elliot, I don't want to be annoying, but I'm kind of new to Unity development (though not to scripting), thus I find this topic interesting in term of learning :) I tested what I was saying (2 instances of a prefab with a simple Destroy( this.gameobject) script), and it perfectly works. What kind of issues did you have?

Show more comments
avatar image
0

Answer by Raymond 2 · Feb 09, 2011 at 08:44 PM

Try this script: var object1 : Transform ; var object2 : Transform ; function Update () {

function OnTriggerEnter (){

Destroy(object1.gameObject); Destroy(object2.gameObject);

   }

}

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 cidmodder · Feb 09, 2011 at 08:49 PM 0
Share

i dont think that would work because they are both the same prefab

avatar image Raymond 2 · Feb 09, 2011 at 08:52 PM 0
Share

But in the scene there will be 2, so then it should work

avatar image cidmodder · Feb 09, 2011 at 10:58 PM 0
Share

eventually though i'm going to want more of the prefabs. like a few hundred at least

avatar image cidmodder · Feb 14, 2011 at 11:45 PM 0
Share

that didnt seem to work. for some reason they won't even spawn anymore when i make them triggers. and I keep getting the expected ( found OnTriggerEnter error when I put it in the update function.

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

Collide detection with tag [JS] 0 Answers

Analysing player metrics help 0 Answers

Getting info about hit Object (Javascript). 1 Answer

2D Collision 1 Answer

NullReference on Collision 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