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 WesterlyCarrot9 · Apr 19, 2013 at 05:32 PM · javascriptcollisiongameobjectdestroyfirst-person-controller

Destroy on Collision?

Ok so i have my FIRST-PERSON CONTROLLER and i want certain cubes to be destroyed as soon as the controllers collides with them. So far i use the below script on the cubes but as soon as i press Play they dissapear. What's the trick here? What kind of if statement do i need? Also when i want to use OnCollisionEnter to destroy something i attach the script to the destroyer or the destroyed? Thanks!

function OnCollisionEnter(){ GameObject.Destroy(gameObject); }

Comment
Add comment · Show 4
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 WesterlyCarrot9 · Apr 19, 2013 at 05:40 PM 0
Share

Ok i used this script attached on the cube which will be destroyed but nothing happens when i collide him with the FP Controller. I created the tag and stuff.

function OnCollisionEnter(collision : Collision){ if(collision.gameObject.tag == ("Destroyer")){ Destroy(gameObject); } }

avatar image HypoXic5665 · Apr 19, 2013 at 05:51 PM 0
Share

Does one of your objects have a rigid-body attached?

" Note that collision events are only sent if one of the colliders also has a non-kinematic rigidbody attached." From OnCollisionEnter documentation

avatar image AlucardJay · Apr 19, 2013 at 05:52 PM 0
Share
  • http://www.unity3dstudent.com/2010/07/beginner-b01-basic-collision-detection/

  • http://www.unity3dstudent.com/2010/07/beginner-b04-destroying-objects/


Here are some links I strongly suggest to all new users :

Start at the bottom and work up : http://www.unity3dstudent.com/category/modules/

this is the YouTube link for the above as one playlist : http://www.youtube.com/watch?v=-oXYHNSmTxg&list=PL27B696FB515608D2&feature=plcp


That is good to get started. Then start with a small tutorial, this is a simple 2D space shooter : http://www.unityjumpstart.com/ProofOfConcept_1/ : click on the videos part1.mp4 part2,3,4 =]

I found another by Eric : http://wiki.unity3d.com/index.php?title=2DShooter : http://forum.unity3d.com/threads/7883-2D-shooter-tutorial

By then you should be getting the hang of things and starting to have ideas of your own. When you decide what kind of game you want to make, then look at each part you'll need. For example, if you want to make some terrain then walk around it with a character : http://cgcookie.com/unity/2011/12/05/introduction-to-character-controllers/

Basically then just search for tutorials, there are many out there, either written or on youtube.

the Unity Wiki tutorials : http://wiki.unity3d.com/index.php/Tutorials

A big list of tutorials : http://answers.unity3d.com/questions/12321/how-can-i-start-learning-unity-fast-list-of-tutori.html

A very helpful 'site, all in C# : http://unitygems.com/

Helpful page with information on using Built-In Arrays and Lists (you'll need this later!) : http://www.unifycommunity.com/wiki/index.php?title=Which_$$anonymous$$ind_Of_Array_Or_Collection_Should_I_Use?

The unity wiki link above is very handy with lots of scripts and shaders too (just check out all the links down the left, and the tabs along the top : http://wiki.unity3d.com/index.php/Scripts )

avatar image EpicGeeth · Jun 23, 2015 at 01:13 PM 0
Share

I am using unity 4 for a school project. You basically have a first person controller, and use it to collect cubes scattered around a map. I need to destroy the cube when the controller collides with them. i have never used c sharp bu

1 Reply

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

Answer by HypoXic5665 · Apr 19, 2013 at 05:52 PM

Currently you have your cubes set to destroy themselves as soon as anything collides with them. In your case I would assume they are colliding with the floor as soon as you start the game and being destroyed. If you are keeping this script on your cubes you will want to do something like:

 function OnCollisionEnter(collision:Collision){
      if(collision.gameObject.tag == "Player"){
           Destroy(this.gameObject);
      }
 }

Be sure that your Player is tagged as "Player" for this example.

Comment
Add comment · Show 16 · 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 WesterlyCarrot9 · Apr 19, 2013 at 05:59 PM 0
Share

I've already tried this. Nothing happens and it really bugs me since this is theoretically the right solution. Do you think it doesn't work because i use a First Person Controller as my player? That's why i wrote it in Capitals above :)

avatar image HypoXic5665 · Apr 19, 2013 at 06:08 PM 0
Share

From Above: Does one of your objects have a rigid-body attached?

"Note that collision events are only sent if one of the colliders also has a non-kinematic rigidbody attached." From OnCollisionEnter documentation

You will need to be sure that neither of the objects are ticked as "isTrigger" in the inspector under their collider properties and, as from the documentation above, be sure that at least one object has a non-kinematic rigidbody attached to it.

[2]: http://docs.unity3d.com/Documentation/ScriptReference/Collider.OnCollisionEnter.html

avatar image AlucardJay · Apr 19, 2013 at 06:12 PM 0
Share

The First Person Controller is an exception to the rigidbody rule.

So the Unity First Person Character controller can activate trigger events without a rigidbody, but for any other object, you need a rigidbody component attached to one of the objects. A basic rule is : if it moves, it needs a rigidbody !

  • http://www.youtube.com/watch?v=ja7z5pYFJXQ

  • http://www.youtube.com/watch?v=n1mgXiYiVWo

  • http://www.youtube.com/watch?v=pktdjxOpgDA

And from my previous comment :

  • http://www.unity3dstudent.com/2010/07/beginner-b01-basic-collision-detection/

  • http://www.unity3dstudent.com/2010/07/beginner-b04-destroying-objects/

avatar image WesterlyCarrot9 · Apr 19, 2013 at 06:14 PM 0
Share

Sorry i forgot to answer that one. Yes the cube that has the script attached to it has a non-$$anonymous$$inematic rigidbody applied as well as the box collider. None of them is Trigger. However the script doesn't work.

avatar image WesterlyCarrot9 · Apr 19, 2013 at 06:22 PM 0
Share

I asked about the FPC because it doesn't have a Capsule Collider but i guess Character Controller is the same since it says there is one already when i try to add a Collider. I don't know what i am doing wrong. Is it possible that the engine itself is bugged?

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

14 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

Related Questions

Destroying a prefab on collision with a cube? 1 Answer

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Collision Detection for a Prefab? 3 Answers

Track debug - Object dissapears after collision 0 Answers

Delete 1 col.gameObject and not both 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