Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 SirMacJefferson · Aug 01, 2011 at 05:49 AM · collisionphysicscolliderenter

OnCollisionEnter() problems

I have a 'Char' prefab that has a capsule called BottomCollider as a child..it's positioned to slightly extend Char's bottom. I wanted to use it so that whenever your 'feet' (BottomCollider, in other words) hit the floor, you can jump again. The jump script is part of Char, and I wanted to set a boolean named jumpValid to True if you touch something with your BottomCollider.

 function OnCollisionEnter()
  {
      
      var charObj = GameObject.Find("Char");
      var charObjJumpScript  = charObj.GetComponent(CharJumpFFR);
      charObjJumpScript.jumpValid = true;
      print("Collision Detected: Under");
      
      
  }

So, CharJumpFFR is the script that has everything jump-related in it.

For some reason, it doesn't seem to be doing anything at all. The print doesn't go off, either. I tried adding a Rigidbody to the cubes that I'm touching with BottomCollider, but it doesn't do anything. If I add a Rigidbody to BottomCollider, it will do something, though. Everything works fine there.

If I add a Rigidbody AND the script component with OnCollisionEnter() to a cube that I'm stepping on, however, it will work. So I suppose the only one that gets the OnCollisionEnter is the one with the Rigidbody attached? Why, if this section in the Unity reference "Box Collider" says:

When a collision between two Colliders occurs and if at least one of them has a Rigidbody attached, three collision messages are sent out to the objects attached to them.

Shouldn't both of the involved colliders get these messages sent?

Thanks for any help you could offer!

Comment
Add comment
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

1 Reply

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

Answer by GuyTidhar · Aug 01, 2011 at 07:49 AM

You did not implement the correct function. OnCollisionEnter() and OnCollisionEnter(collision : Collision) can both exist and are not the same function (yours is missing an argument). Unity defines OnCollisionEnter(collision : Collision) and not OnCollisionEnter() . Therefor when you define the function OnCollisionEnter() you defined a new function which unity does not call instead of implementing the function unity does call.

 function OnCollisionEnter(collision : Collision)
 {
    var charObj = GameObject.Find("Char");
    var charObjJumpScript  = charObj.GetComponent(CharJumpFFR);
    charObjJumpScript.jumpValid = true;
    print("Collision Detected: Under");
 }
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 SirMacJefferson · Aug 01, 2011 at 08:11 AM 0
Share

Thanks! It's working fine now. I removed the BottomCollider because it wasn't really necessary if I could just expose the bottom of the Char and I gave the Char the script.

avatar image GuyTidhar · Aug 01, 2011 at 08:52 AM 0
Share

Glad I could help.

Also note, that you should really consider doing Find and GetComponent calls only once (when possible) and keep the return types in memory.

avatar image SirMacJefferson · Aug 01, 2011 at 11:39 PM 0
Share

I see. I'll keep that in $$anonymous$$d and change the code. Thanks again! :)

avatar image SirMacJefferson · Aug 02, 2011 at 12:25 AM 0
Share

I changed the code to this:

var charObj:GameObject = GameObject.Find("Char"); var charObjJumpScript = charObj.GetComponent(CharJumpFFR); function OnCollisionEnter(collision:Collision) {

      charObjJumpScript.jumpValid = true;
      Debug.Log("Collision Detected: Under");
      
      
  }

but now it comes up with these errors:

UnityEngine.GameObject:Find(String)

and...:

ArgumentException: Find can only be called from the main thread. Constructors and field initializers will be executed from the loading thread when loading a scene. Don't use this function in the constructor or field initializers, ins$$anonymous$$d move initialization code to the Awake or Start function.

and...:

UnityException: You are not allowed to call this function when declaring a variable. $$anonymous$$ove it to the line after without a variable declaration. If you are using C# don't use this function in the constructor or field initializers, Ins$$anonymous$$d move initialization to the Awake or Start function.

Why isn't it letting me call that function? Do I have to call it inside OnCollisionEnter?

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Rigidbodies won't collide if mass difference is too high 0 Answers

Detailed terrain mesh collision 1 Answer

OnCollisionEnter isnt called when player lands on object 1 Answer

early collision above character controller 2 Answers

Collision Issue - Walk Through Object 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