GetComponent Help

#pragma strict
var player = GameObject.Find(“Controller”);
var death:AudioSource;

function Update () {
var healthgui = player.GetComponent("_HealthGui");
if(healthgui.health < 1){death.Play();}
}

//I’ve been getting this error where it says “health is not a valid member of UnityEngine.Component”. I really need help, and GetComponent never works for me, and I have no idea why.

Don’t use quotes in GetComponent; that makes it return Component instead of _HealthGui. Also you might want to cache that instead of doing that every frame in Update.

This was just happening to me! I only started with Unity a few weeks ago. The problem is that Find won’t run outside of a function. The simple solution is to declare your “var player” in the global scope, at the top, where it is now. And then put the GetComponent call on it in the Start() function. From what I surmise, Start() is run once when the object is instantiated, and is the exact place for this sort of function call.