MissingMethodException: UnityEngine.Collision.GetComponent

It’s supposed to check whatever it hits to find if there is a script named HealthSystem and a value named Health or not but it doesn’t work. Any ideas of another way to do this?

function OnCollisionEnter(hit){
    Destroy (gameObject);
    
    var Health = hit.GetComponent(HealthSystem).Health;
    if(Health) {
    	Debug.Log(Health);
    }
}

You need to declare your variable ‘hit’ type. You are also using the if statement incorrectly. Not sure why you’re destroying the object before you execute the rest of the code, destroying should come last. You also need to fix your GetComponent statement where you try to call a variable from the script. You should also define variable outside your functions. Try something like this.

var Health;

function OnCollisionEnter(other : Collision)
{
    if(other.collider.GetComponent(HealthSystem)) 
    {
        Health = other.collider.GetComponent(HealthSystem).Health;
        Debug.Log(Health);
    }
    
    else
    {
        Debug.Log(other.collider.name + " Does not have HealthSystem script attached! ");
    }
    //Destroy (gameObject);
}