Problem porting JS to CS

I’m porting a short JS script to CS.

The JS script is:

function OnCollisionEnter(collision : Collision) 
{
    var debris = collision.collider.GetComponent("Debris");
    if (debris)
    {
        Destroy(this.gameObject);
    }
}

In, C#, I wrote it as

void OnCollisionEnter(Collision collision)
    {
        Component debris = collision.collider.GetComponent("Debris");

        if (debris)
        {
            debris.GiveMass();
            Destroy(this.gameObject);
        }
    }

However, this gives me the error:

Assets/Scripts/Player/PrimaryShotCollider.cs(13,20): error CS1061: Type `UnityEngine.Component' does not contain a definition for `GiveMass' and no extension method `GiveMass' of type `UnityEngine.Component' could be found (are you missing a using directive or an assembly reference?)

The Debris component, also in CS, has a GiveMass() method which is public. The JS version of the script has no problem calling that method, but the CS version gives the abovementioned error.

1 Answer

1

Nevermind, figured it out. If anyone is wondering, the first line inside the method should read:

Debris debris = collision.collider.GetComponent<Debris>();