Home > OS >  unity not recognising a collision
unity not recognising a collision

Time:09-01

enter image description here

enter image description here

2 snippets of code from 2 separate scripts
    protected virtual void OnCollide(Collider2D coll)
    {
        Debug.Log("OnCollide was not implemented in"   this.name);
    }
}



    protected override void OnCollide(Collider2D coll)
    {
        if (coll.tag == "Fighter")
        {
            if (coll.name != "Player")
            {
                Debug.Log(coll.name);

            }
        
        }
    }
private void Swing()
{
    Debug.Log("Swing");
}

}

running into a problem that when I collide with an enemy (there is meant to be a console notification) nothing happens, no errors appear, I cant seem to find any problems with my code, the collision is registered with my test npc but not the enemy

CodePudding user response:

As BugFinder and sorold both said, you named the method wrong. It is supposed to be OnCollisinEnter or OnCollisionEnter2D based on which version you use.

Unity checks if you have specific methods in your script. If you do not, they won't call that method, but if Unity recognizes that method name, it will be called.

CodePudding user response:

I had the same problem but when i added a rigidbody to it or to that colliding object, it fixed.

  • Related