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.
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);
}
}
}
}
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”