- Home /
OnCollisionEnter not working?
I know this question is asked a lot, and I've had this problem before ,but nothing I can find solves my problem. I have a simple script, so that when an object (tree) is hit with an axe, it will drop a piece of wood. However, It doesn't ever even count as getting hit. The axe is attached to the FPS Controller, and i'm wondering if that's where my issue stems from. I've also tried OnTriggerEnter, doesn't work. I've had the tree with a rigidbody, every piece with a rigid body, the axe and every component with a rigid body, "is trigger" on and off, but nothing works. What should I do? Here's my script:
// Use this for initialization
void Start () {
Debug.Log("test");
}
// Update is called once per frame
void Update () {
if(woodcounter >= 15)
{
Instantiate(wood, new Vector3(a, 0.5f, a), Quaternion.identity);
a++;
woodcounter = 0;
}
}
public GameObject wood;
public float a;
public float woodcounter;
void OnCollsionEnter(Collision other)
{
Debug.Log("1");
if(other.gameObject.tag == "axe")
{
Debug.Log("2");
woodcounter++;
}
}
}
Thanks for the help! This script is attached to the tree, in case that matters. Both the axe and tree are under an empty game object that holds all the child parts, but I don't think that should matter. And yes, i've tried changing the script so that it is on all parts the tree.
Thanks! :)
@ShadowNukePie I'm just guessing here but don't spend too much time working on this functionality thinking you will transfer it to Terrain trees because that won't work; search for solutions here on UA if you are planning that.
Answer by tanoshimi · Dec 18, 2016 at 07:47 AM
void OnCollisionEnter
not
void OnCollsionEnter
Thanks! It works :D Can't believe i missed something that simple! I wish that would create an error. :/
There's no reason for it to create an error, because it's not wrong. You can name custom functions (nearly) however you want. But if you want them to get called by Unity, you need to name them exactly the same as Unity's callback system expects.
^ This. Thankfully Visual Studio with Unity Tools now has the On... functions with intellisense.