- Home /
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.
Answer by Eric5h5 · Sep 15, 2012 at 12:54 AM
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.
Okay, I removed the quotations and now fixed the every frame issue, but now I get an error "$$anonymous$$ identifer: '_HealhGui'.
Don't post comments as answers please (converted to comment). If that's what it's actually saying, I guess you made a typo (check your spelling). Otherwise, you have no such class available, and need to use the actual name of whatever the class is called.
What is a class exactly? $$anonymous$$y script is called "_HealthGui", and it's attached to the player itself. I'm a newbie, I'm sorry.
If you're mixing languages, http://docs.unity3d.com/Documentation/ScriptReference/index.Script_compilation_28Advanced29.html
Answer by ShiftedClock · Sep 15, 2012 at 06:55 AM
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.
I still somehow got the unknown identifier variable. I may have it wrong, however.
pragma strict
ar player = GameObject.FindWithTag("Player"); var death:AudioSource; private var debounce = false; function Start () { var healthgui = player.GetComponent(_HealthGui); }
function Update () { if(debounce == false){ debounce = true; if(healthgui.health < 1){death.Play();} }}
Ah, I remember now, I had the same problem. What fixed it for me was proper type casting. For example:
var player = GameObject.Find("Controller");
var death:AudioSource;
function Update () {
var healthgui = player.GetComponent("_HealthGui");
if(healthgui.health < 1){death.Play();}
}
becomes:
var player : ControllerType = GameObject.Find("Controller");
var death : AudioSource;
function Update () {
var healthgui : ProperType = player.GetComponent("_HealthGui");
if(healthgui.health < 1){death.Play();}
}
Notice the "ControllerType" and "ProperType", those are just placeholders, I don't know the proper type to cast for them. But I do know that by not giving it a type, it casts your player object to some default, probably UnityEngine.Component. Then you're looking for _HealthGui on that type of object, which it doesn't have.
At least, damn I hope I'm right this time. I just fixed this error in the game I'm working on, and I'm pretty sure this is how I fixed it. Good luck!