Getting error "There is no collider attached to enemy" (but there is)

Like topic headline: When running the game and my projectile hits the enemy i get the error:

MissingComponentException: There is no ‘Collider’ attached to the “Lazer1(Clone)” game object, but a script is trying to access it.
You probably need to add a Collider to the game object “Lazer1(Clone)”. Or your script needs to check if the component is attached before using it.
EnemyBehaviour.OnTriggerEnter2D (UnityEngine.Collider2D mincol) (at Assets/Enteties/Enemy/EnemyBehaviour.cs:12)

But the lazer1 does have a box collider 2d attached to it.

This is the code on the enemyBehaviour script:

void OnTriggerEnter2D(Collider2D mincol) {
    Projectile misil = mincol.GetComponent<Collider>().gameObject.GetComponent<Projectile>();
    if (misil)
    { //if misil existerar, inte är null eller false
        Debug.Log("treffad av misil");

Hello :slight_smile:
Unity handles 2D colliders and 3d colliders as diferent components, use the same collider type from the inspector in the GetComponent. Since the laser has a 2d collider, you should use it this way:

mincol.GetComponent<Collider2D>()

By the way, sorry for the delay to answer, Im working hard on my current game :slight_smile: let me know if this solves your problem

2 Likes

Thanks so much, now it works! :slight_smile:

2 Likes

Oh, Im glad to know that it worked, let me know if I can be of further help