Home > Net >  I have tried to fix this code but I don't know how to
I have tried to fix this code but I don't know how to

Time:08-18

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

public class Collidable : MonoBehaviour
{
    public ContactFilter2D filter;
    private BoxCollider2D boxCollider;
    private Collider2D[] hits = new Collider2D[10];

    protected virtual void Start()
    {
        boxCollider = GetComponent<BoxCollider2D>();
    }

    protected virtual void update()
    {
        // Collision work
        boxCollider.OverlapCollider(filter, hits);
        for (int i = 0; i < hits.length; i  )
        {
            if (hits[i] == null)
                continue;

            Debug.Log(hits[i].name);

            // The array is not cleaned up, so we do it ourself
            hits[i] = null;
        }
    }
}

The error says: error CS1061: 'Collider2D[]' does not contain a definition for 'length' and no accessible extension method 'length' accepting a first argument of type 'Collider2D[]' could be found (are you missing a using directive or an assembly reference?) I'm new to coding. Please help me.

CodePudding user response:

make sure you use capital "L" . Methods are case sensitive in c# See Array.Length method https://docs.microsoft.com/en-us/dotnet/api/system.array.length?view=net-6.0 .

But how such a simple typo missed from your IDE? Are you using intellisense? Just hitting "hits." should point to all the available methods.

  • Related