- Home /
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);
}
}
Comment
Best Answer
Answer by clunk47 · Aug 14, 2013 at 03:03 AM
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);
}
Thanks! The logic makes perfect sense, I was just trying to mix logic from my primary language in.
Your answer
![](https://koobas.hobune.stream/wayback/20220613112149im_/https://answers.unity.com/themes/thub/images/avi.jpg)