|
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. 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:
Shouldn't both of the involved colliders get these messages sent? Thanks for any help you could offer!
(comments are locked)
|
|
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. 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.
Aug 01 '11 at 08:11 AM
SirMacJefferson
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.
Aug 01 '11 at 08:52 AM
GuyTidhar
I see. I'll keep that in mind and change the code. Thanks again! :)
Aug 01 '11 at 11:39 PM
SirMacJefferson
I changed the code to this: var charObj:GameObject = GameObject.Find("Char"); var charObjJumpScript = charObj.GetComponent(CharJumpFFR); function OnCollisionEnter(collision:Collision) { 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, instead move initialization code to the Awake or Start function. and...: UnityException: You are not allowed to call this function when declaring a variable. Move 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, Instead 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?
Aug 02 '11 at 12:25 AM
SirMacJefferson
(comments are locked)
|
