OnCollisionEnter2D is declared but never used.

Following the Ruby tutorial, and this text (from the tutorial) produces the title warning in the compiler:

void OnCollisionEnter2D(Collision2D other)
{
RubyController player = other.gameObject.GetComponent();
if (player != null)
{ player.ChangeHealth(-1);
}
}

Also, it doesn’t actually work like it says it should…

(World Interactions - Damage Zones and Enemies - Unity Learn for reference).

Please ELI5 in your replies :slight_smile:

Thanks,
J

My first guess is that you forgot to subclass MonoBehaviour (public class MyClass : MonoBehaviour). Beyond that, the entire class you’re working with (in a code block!) would be helpful.

No idea what ELI5 means.

Whole of the code is here:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyController : MonoBehaviour
{
public float speed = 3.0f;
public bool vertical;
public float changeTime = 3.0f;
Rigidbody2D rigidbody2D;
float timer;
int direction = 1;
// Start is called before the first frame update
void Start()
{
rigidbody2D = GetComponent();
timer = changeTime;
}
// Update is called once per frame
void Update()
{
timer -= Time.deltaTime;
if (timer < 0)
{
direction = -direction;
timer = changeTime;
}
Vector2 position = rigidbody2D.position;
if (vertical)
{
position.y = position.y + Time.deltaTime * speed * direction;
}
else
{
position.x = position.x + Time.deltaTime * speed * direction;
}
rigidbody2D.MovePosition(position);
void OnCollisionEnter2D(Collision2D other)
{
RubyController player = other.gameObject.GetComponent();
if (player != null)
{
player.ChangeHealth(-1);
}
}
}
}

Explain Like I’m 5. :slight_smile:

Unity methods will often result in that warning because they’re not explicitly referenced in code, Unity will call them automatically thru other means behind the scenes.

If your class inherits from MonoBehaviour, and your function signature is written correctly (which it appears to be), then it’s possible your objects aren’t set up properly to collide with each other.

@zioth ELI5 means “explain it like I’m five years old”

In the future, please put code in a code block. It makes it much easier to read (see the word “Code” in the post editor toolbar).

Your code looks okay. Some other things to look at:

  • Does your player have a collider? How about your enemy?
  • Make sure your colliders don’t have “is trigger” checked. If you’re using triggers, you’ll need OnTriggerEnter2D instead of OnColliderEnter2D.
  • Are you sure the colliders are touching? Maybe one is way too small or something?
  • Are the scripts attached correctly?
  • Are the scripts enabled? (checkbox in the upper-left corner of the script component).
  • Check the sort layer of each object, and look at the project settings. Did you disable collisions between the two layers?

You got your “}” messed up, so your OnCollisionEnter2D is inside the Update.

4 Likes

I got the same problem and this is the solution!

woww, thx dude, works perfectly!!!

Not to necro a thread, but this was literally the problem I was having, thanks haha