- Home /
OnCollisionEnter not working with Platform and Ball game
Hello, I have a Ball which moves based on user input with a rigidbody and a collider, and I have a platform named "Start", which is where the ball is first placed. Start has a collider, but no rigidbody
I have a script attached to the Start platform named Start.cs, which is supposed to say something in the log if anything collides with it.
Here is the code for Start.cs using UnityEngine; using System.Collections;
public class Start : MonoBehaviour {
// Update is called once per frame
void Update () {
}
public void onCollisionEnter(Collision collision)
{
Debug.Log("The battle has begun");
}
}
Apparently, nothing shows up in the Log with my code. However, I have another code attached to the Ball named Movement.cs which has a Working OnCollisionEnter function, which can actually leave something in the log if it collides with something.
Below is the part of the code of Movement.cs, particularly the portion with OnCollisionEnter()
using UnityEngine; using System.Collections;
public class Movement : MonoBehaviour {
bool canjump = true;
int noofcollisions = 0;
void OnCollisionEnter(Collision collision)
{
canjump = true;
Debug.Log("Collided with " + collision.gameObject.name + " " + noofcollisions + " times");
noofcollisions++;
if(collision.gameObject.name == "Finish")
{
Debug.Log("Congratulations!!! You have won the game!!!");
}
//calculate distance of jump
//Debug.Log(GameObject.Find("Ball").transform.position.x);
}
}
What causes the OnCollisionEnter function of my Start platform to not work? How can I fix this problem? Cheers.
Answer by kolban · Apr 14, 2012 at 01:10 PM
When writing Unity JavaScript or C# scripts, it is very important to check spelling and case of methods calls. A function called Foo() is not the same as a function called foo().
Looking at your original code, you have coded:
public void onCollisionEnter(Collision collision)
where what I believe you mean to code was
public void OnCollisionEnter(Collision collision)
Note the first character of the function name.
Amazing!!! I missed the capitalized "O". That solved the problem. Now something appears in the log. It works.
Thank you!