Inheritance Not Working

Hey all, first time posting, please be gentle.

So I’m currently running Unity v2018.4.10f1, and I had this working fine a few days ago but today I booted up my project and it is no longer working. I’ve watched tutorials and walkthroughs, relevant YouTube videos but nothing seems to stick out.

Basically I have a player sprite that walks over a key sprite and the collision with the key’s box collider is supposed to trigger my Collidable script which goes through a few scripts of inheritance that will turn the key sprite rendered to false.

Here is the main Collidable script;

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

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


    public virtual void Start() {

        boxCollider = GetComponent<BoxCollider2D>();
    }

    public virtual void Update()
    {
        //Collision work
        boxCollider.OverlapCollider(filter,hits);
        for (int i = 0; i < 10; i++) {
            if(hits[i] == null)
                continue;

            OnCollide(hits[i]);
            playerCollision(hits[i]);

            //Clear array
            hits[i] = null;
        }
    }


    public virtual void OnCollide(Collider2D coll)
    {
        Debug.Log(coll.name);

    }

Which in turn should send to a collectable script;

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

public class Collectable : Collidable
{
   public bool collected;

   public override void OnCollide(Collider2D coll)
   {
       if (coll.name == "Player_0")
           OnCollect();
   }

   public virtual void OnCollect() {
       collected = true;
   }
}

Which then goes to the key’s script;

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

public class Key : Collectable
{

    public Sprite emptyKey;

    public override void OnCollect()
    {
        if (!collected) {
            collected = true;
             this.GetComponent<Renderer> ().enabled = false;
        }
    }
}

I put in some debug logs to send messages to console if anything other than my initial Collidable script gets any hits, but am having no such luck. Like I said before, this worked a few days ago but today it seems to be bricked. Any advise would be appreciated.

Hello BurninIt and welcome to our forum!

What you could do is either place a break point where you have your for-loop, or print all collider names out inside the for-loop, and make sure that the initial array of hits actually contains the colliders you are looking for. If they do not, make sure that your filter is correctly setup, and that the game objects have both 2D colliders and 2D rigidbodies.

Also note that since you are overriding the OnCollect()-method, your parent classes method will not be called, unless you add base.OnCollect(); in the override OnCollect methods.

Keep us posted on your progress!

1 Like