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 johny_jordan · Apr 27, 2012 at 01:02 AM · destroy object

Destroy Projectile on Impact?

I'm a noob making a disc golf game. I think I'd have trouble getting the discs to stick in the chains on impact, so I'd like to just destroy the discs on impact with the chains. The chains have a sphere collider and it's marked as a trigger. I also have an explosion set to detect collision. The collision is working.

However, I'm having a hard time figuring out how to simply destroy the discs. All discs are tagged as "discs". They're defined in a different script. Here is my code:

var explosion : Transform;

function OnTriggerEnter(hit : Collider)

{ if(hit.gameObject.tag == "discs") {

 Destroy gameObject.tag == "discs";
         
     var exp = Instantiate(explosion, gameObject.transform.position, Quaternion.identity);    
         
                 
             
                     StrokeCount.STROKES = 0;
                     StrokeCount.BASKETHITS += 1;
 }

}

Trust me, I understand that I'm noob. I appreciate any help that's offered.

Comment
Add comment · Show 1
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 johny_jordan · Apr 27, 2012 at 12:03 PM 0
Share

O$$anonymous$$, here's a little update/change of attack...

I created a different script entirely and oriented it as the kill disc on collision script. I've attached this to all discs.

function OnCollisionEnter(collision : Collision) {

 if(collision.gameObject.tag == "chains")
 {
 Destroy(gameObject);
 }

}

This doesn't destroy the disc and I've got to be missing something simple in my logic. I can get the disc to destroy when it collides with anything, but I'm having trouble getting it to ONLY destroy it when it collides with a specific collider, which is tagged "chains".

I appreciate the help so far. It's helped me understand why my previous method wasn't working.

3 Replies

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

Answer by aldonaletto · Apr 27, 2012 at 01:39 PM

If your original script was attached to the disc, it should be:

var explosion : Transform;

function OnTriggerEnter(hit : Collider){ if (hit.tag == "chain") { // if hit a chain object... Destroy(gameObject); // destroy himself... // and instantiate the explosion in its place: var exp = Instantiate(explosion, transform.position, Quaternion.identity);
StrokeCount.STROKES = 0; StrokeCount.BASKETHITS += 1; } } Your second script will not work unless you uncheck Is Trigger in the chain collider: OnCollision events only occur when the rigidbody hits a regular collider - OnTrigger events are reported when the collider has Is Trigger checked.
Your disc script could be something like this:

var explosion : Transform;

function OnCollisionEnter(col : Collision){ if (col.gameObject.tag == "chain") { // if hit a chain... Destroy(gameObject); // destroy itself // instantiate the explosion at its own position: var exp = Instantiate(explosion, transform.position, Quaternion.identity);
StrokeCount.STROKES = 0; StrokeCount.BASKETHITS += 1; } }

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 johny_jordan · Apr 27, 2012 at 05:26 PM 0
Share

This works perfectly. Thanks so much for your help!

avatar image
0

Answer by kolban · Apr 27, 2012 at 01:25 AM

Hi there and welcome. The Unity Answers forum is more for focused Q&A on Unity as opposed to general discussion topic so you may want to look at the Unity Community forums for questions such as this. Looking at your code, I see that you have a line which reads:

 Destroy gameObject.tag == "discs";

I am actually surprised this even compiles. The syntax for the Destroy command can be found here:

http://unity3d.com/support/documentation/ScriptReference/Object.Destroy.html

As you will see, it expects an Object as a parameter and this is commonly an instance of GameObject to be destroyed. In your code, you are passing in:

 gameObject.tag == "discs"

Which is a "boolean" (a true/false value) and most certainly not an object. As such, your discs are not disappearing.

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 johny_jordan · Apr 27, 2012 at 07:42 AM 0
Share

It actually doesn't compile, I just wanted to show my train of thought. Sorry for not mentioning that. I can't seem to write a Destroy that works unless it's actually destroying the chains, not the disc.

From your answer, I do understand why it's not working, though.

avatar image
0

Answer by McDardy · Apr 27, 2012 at 08:36 AM

From what I understand this destroying projectiles on impact is very similar behaviour to shooting rockets that explode when colliding, right?

http://unity3d.com/support/resources/tutorials/fpstutorial.html

I don't know why this tutorial is hided for people. It's pretty old but when you're searching for some ideas it will do... And some colliding/destroying things you can also find in... Lerpz 3D platform tutorial:

http://unity3d.com/support/resources/tutorials/3d-platform-game

I found also this:

http://unity3d.com/support/documentation/ScriptReference/Object.Instantiate.html

And I guess that will be the best resource for your problems. It's overcommented so noone should have problems with understanding and implementing this in their projects.

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

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Destroy Objects to Load New Scene 3 Answers

Why does Destroy(this.gameObject) destroys a prefab that got nothing to do with the script 0 Answers

Change Score / On Click Destroy Enemy 1 Answer

Problem deleting object 1 Answer

detect Gameobject which is created from c# 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