• 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 Adam 4 · Aug 25, 2010 at 12:04 AM · colliderdestroy

What Am I Doing Wrong

So im trying to make my player destroy a cube when he hits it I compile my code and i have no errors but it still dosen't work. Heres my code:

function OnTriggerEnter( hit : Collider ) { if(hit.gameObject.tag == "Player") { Destroy(gameObject); } }

If anyone can explain to me what i did wrong i would really appreciate it.

Comment
Add comment · Show 2
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 · Aug 25, 2010 at 12:31 AM 0
Share

When posting code, select the snippet and press the button with 0's and 1's on it - it makes it easier to read (even though this example is quite small).

avatar image · Aug 25, 2010 at 12:40 AM 0
Share

Also, try to be more specific when na$$anonymous$$g your questions. "What Am I Doing Wrong" doesn't tell anyone what you're actually asking.

2 Replies

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

Answer by · Aug 25, 2010 at 12:40 AM

It's unclear from your question - the script would need to be attached to the cube(s), not the player. If so, is your player object definitely tagged as "Player"? Does the player / cube have a rigidbody? Is the IsTrigger property set to 'True'?

One suggestion I'd make for general debugging is to use Debug.Log. You could modify your code to quickly rule out a few potential issues.

function OnTriggerEnter( hit : Collider )
{
   Debug.Log("Hit was triggered");
   if(hit.gameObject.tag == "Player")
   {
      Debug.Log("Collider hit was Player");
      Destroy(gameObject);
   }
}

You can then view your log and see which messages appeared. If the second message didn't show, the problem most likely lies with the tagging, or that the script is on the player and checking for the "Player" tag on the cubes.

Let me know how you go.

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 Adam 4 · Aug 25, 2010 at 01:13 AM 0
Share

Thank you for replying. Yes the script is attached to the cube, it is a rigidbody, my player object is tagged as player and the Is Tagged is set to true.The only thing is when i set the Is Tagged to true my cube falls through the ground. Thanks for your help.

avatar image · Aug 25, 2010 at 01:56 AM 0
Share

Try setting the useGravity property on the Cube to be 'false'. Also, while I appreciate the 'accepted answer', I haven't quite solved your problem yet! I'll have access to Unity for the next hour or so, if there are still problems.

avatar image Adam 4 · Aug 25, 2010 at 02:47 AM 0
Share

The box is destroyed when i touch it now i guess i just can't use gravity, but thats alright. Thank you for all your help and dealing with my major noobness.

avatar image · Aug 25, 2010 at 03:55 AM 0
Share

Turning useGravity off should just be a workaround. I'm not sure why it falls through your ground - does your ground have collision on it? Good luck with your project!

avatar image
0

Answer by Adam 4 · Aug 25, 2010 at 05:53 AM

Thanks! From what i have read so far it seem like once you add a script to an object it trumps all physics made by unity.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Player colliding with object then destroying/killing Player 2 Answers

Destroy objects when key is pressed 1 Answer

Disabling or Destroying more lagless/ Do colliders and transforms work while disabled? 1 Answer

Remove trees dynamically - stuck with collider 3 Answers

Why are collisions not working? 3 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