error CS0029: Cannot implicitly convert type 'UnityEngine.Collider2D' to 'UnityEngine.Collider2D[]'
Asked Answered
W

1

0

Hello, I am having an issue with making melee combat work. I am following a tutorial by Brackeys on melee combat, but when I try to compile this code, I get error CS0029: Cannot implicitly convert type ‘UnityEngine.Collider2D’ to ‘UnityEngine.Collider2D’. I am not sure how to fix this. Everything I’ve tried has not worked, and every answer to similar situations I have found I have been unable to properly understand as my knowledge of programming is extremely limited. Thank you for any help you are able to provide.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerCombat : MonoBehaviour
{

public Animator animator;

public Transform attackPoint;

public LayerMask enemyLayers;

public float attackRange = 0.5f;
public int attackDamage = 1;


// Update is called once per frame
void Update()
{
    if (Input.GetKeyDown(KeyCode.N))
    {
        Attack();
    }
}

void Attack()
{
    //play an attack animation
    animator.SetTrigger("Attack");

    //detect enemies in range of an attack
   Collider2D[] hitEnemies =  Physics2D.OverlapCircle(attackPoint.position, attackRange, enemyLayers);

    //damage them
   foreach(Collider2D enemy in hitEnemies)
    {
        enemy.GetComponent<Enemy>().TakeDamage(attackDamage);
    }
}

public void OnDrawGizmosSelected()
{
    if (attackPoint == null)
        return;

    Gizmos.DrawWireSphere(attackPoint.position, attackRange);
}
Whitworth answered 10/7 at 21:41 Comment(0)
B
0

Greetings, @Whitworth

I’ll fix this for you in a moment but first a few tips on how to find the error yourself. If you go to the manual on Physics2D.OverlapCircle, you’ll see that there are three “Declarations” (versions). Interestingly, that’s different from Physics.OverlapSphere (the 3D version), where there is only one declaration. But that’s another topic.

How can one method have three different versions? It’s to do with there being three different sets of arguments that you can pass. The C# Compiler is clever enough to know which of the three versions you want based on which types of arguments you give it. This process is called Overloading.

The first definition returns a single Collider2D - importantly not an array of colliders. That’s why you’re getting an error. The error message says that the compiler can’t convert a single collider into an array. Of course, you want to get all the colliders, not just the first it finds and that brings us on to the other Definitions. You are “choosing” this first definition because your third argument is a LayerMask. It’s the only one of three definitions that has LayerMask for the third argument.

The second and third definitions have an extra argument but note that they return an integer. That integer is the number of colliders that they find. What does it do with the actual colliders? It puts them in an argument that you need to provide as the fourth parameter. If you pass an Array of Colliders as the fourth parameter, you are given the results in that array. If you pass a List of Colliders (a List is a more flexible type of array), again it will populate the List with Colliders.

So, you need to use the second definition but there is one more thing that you need to change. The third parameter is not a LayerMask but a different Class called ContactFilter2D A ContactFilter2D lets you specify a lot more detail in what type of collision you want. However, since you are not using LayerMask at all, you can delete it and replace its definition with ContactFilter2D contactFilter;.

We’re nearly there. Remember that the parameters to Physics2D.OverlapCircle are very specific and it returns in integer. So, you’ll need the following code instead:

        Collider2D[] hitEnemies = new Collider2D[100];
        int hitCount = Physics2D.OverlapCircle(attackPoint.position, attackRange, contactFilter, hitEnemies);

You’ll see that I had to explicitly create the array and give it a size. The size determines the maximum number of colliders that it will return. You can pick a smaller number, depending on how many enemies you have in your game. That’s it. you should now be able to get past that error.

Bose answered 10/7 at 21:41 Comment(2)

I'm in love....

Rosenberry

Thank you so much for your help, that fixed the errors right away.

Whitworth

© 2022 - 2024 — McMap. All rights reserved.