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 Alex 10 · Dec 02, 2010 at 07:49 PM · collisionobjectgamedestroy

Problem with collisions and Destroy(gameObject).

I'm making a Space Invaders clone for my first unity project so I can get comfortable with the engine. I'm trying to make the bullet colliding with the ship destroys the ship. Here is the code:

function OnControllerColliderHit (hit:ControllerColliderHit)
{
        if (hit.gameObject.tag == "bullet")
        {
                Destroy(gameObject);    
        }
}

I have the code attached to the ship and the bullet has the "bullet" tag. My question is why isn't the ship being destroyed?

Edit:

I changed the code to:

function OnControllerColliderHit (hit:ControllerColliderHit)
{
        Debug.Log("Collision Works");if (hit.gameObject.tag == "bullet")
        {
                Destroy(gameObject);    
        }
}

It did not say the collision works in the console. Thanks for all the help by the way =)

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 Rennat · Dec 03, 2010 at 06:34 PM 0
Share

@$$anonymous$$, if your question has been answered please mark the most helpful answer as accepted

2 Replies

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

Answer by Peter G · Dec 03, 2010 at 04:20 AM

If the debug isn't being called then the function isn't running.

So, make sure you do in fact have a CharacterController on the ship. Otherwise, you should use OnCollisionEnter().

Make sure you are moving your bullets using Unity's physics engine. If you use Transform.Translate() to move your bullets then the message won't be sent either.

For the character controller, you should use exclusively Controller.Move().

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 Alex 10 · Dec 03, 2010 at 06:01 PM 0
Share

thanks guys, it works now.

avatar image
1

Answer by Rennat · Dec 02, 2010 at 08:01 PM

to narrow down where your code is not working you can use Debug.Log(message); to let you know what is actually happening. because with this code either OnControllerColliderHit is not getting called or the hit.gameObject.tag is never "bullet".

logging a message "collider hit" inside OnControllerColliderHit would let you know that it is in fact getting called.

Comment
Add comment · Show 2 · 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 Alex 10 · Dec 02, 2010 at 09:22 PM 0
Share

Sorry I'm new to this. I searched for a while on google and can't find how to work the Debug.Log. Could you tell me how that works?

avatar image Rennat · Dec 02, 2010 at 10:47 PM 0
Share

just add the line Debug.Log("hi"); and it will print "hi" to the console (the window where code error notifications pop up) letting you know that line of code is getting executed, or the value of a variable with something like Debug.Log("speed: " + speed);

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

Destroying Gate by hitting a switch 1 Answer

Make an object destroy the object it is touching? 2 Answers

Destroy object if collision is strong enouf 1 Answer

Clones of object wont disapear.. 1 Answer

If two of the same objects spawn on top of each other, is it possible to destroy only one? 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