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 Xedfire 1 · Feb 24, 2011 at 01:16 PM · physicsrigidbodyfunctiononcollisionenteraddcomponent

OnCollisionEnter problem

Basically, I am making a game where the player has to dodge flying cubes... But I can't seem to get one feature to work. I want to "activate" or "add" a rigidbody to the player when one of the flying cubes hit it, but everything that I have tried has not worked... This is the code that people elsewhere have used:

function OnCollisionEnter(hit : Collision) { if(hit.gameObject.tag == "cube") { Debug.Log("Hit"); gameObject.AddComponent(RigidBody); }

}

but this isn't working for me :(

Can anybody help? I don't mind if I'm making a really noobish mistake.

Thanks in advance.

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 Joshua · Feb 24, 2011 at 01:52 PM 0
Share

is the Debug.Log working though?

3 Replies

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

Answer by Joshua · Feb 24, 2011 at 02:45 PM

I think I figured it out (the other two answers are absolutely wrong)

You need to remove the capital B in RigidBody to make it Rigidbody. Rigidbody is the function you want to attached, rigidbody is the variable.

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 PrimeDerektive · Feb 24, 2011 at 02:58 PM 2
Share

An additional note: from his description, I'm assu$$anonymous$$g his snippet is in a script on the player. Note that OnCollisionEnter() will never fire if the flying cubes do not already have Rigidbody components.

avatar image
0
Best Answer

Answer by Xedfire 1 · Mar 05, 2011 at 04:33 PM

Ah, I now know what went wrong. You were all right, so thanks to everyone who posted answers. Basically the problem was a bug (or a complicated feature) in the engine (or my version of the engine)... it only happened when I named my script "collision" (?), and in doing so, I got a strange warning message saying: "This message parameter has to be of type: Collision". It might be just a bug in my downloaded version of Unity (3.2.0), but when I renamed my script to something else, it was fine.

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
-2

Answer by rimawi · Feb 24, 2011 at 01:44 PM

Hi Try

function OnControllerColliderHit(hit: ControllerColliderHit) { if(hit.gameObject.tag=="cube"){

{ Debug.Log("Hit");

} }

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 Joshua · Feb 24, 2011 at 01:51 PM 0
Share

I wouldn't try that. OnControllerColliderHit only works if both the player and the object have a velocity. OnCollisionEnter just always works.

avatar image Meltdown · Feb 24, 2011 at 02:14 PM 0
Share

How does that help his request? It doesn't add or activate any sort of rigidbody.

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

RigidBody 2D x velocity function 1 Answer

Script isn't consistent 0 Answers

,Fast Moving Gameobject Collision and Trigger Check Failed 0 Answers

Don't understand why this on collision script doesn't work. 2 Answers

Need second pair of eyes: rigidbodies with colliders not firing OnCollisionEnter yet again... 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