Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by AngryCSharpNerd · Sep 15, 2012 at 12:42 AM · guigetcomponenthealth

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AngryCSharpNerd · Sep 15, 2012 at 02:55 AM 0
Share

@eric

Okay, I removed the quotations and now fixed the every frame issue, but now I get an error "$$anonymous$$ identifer: '_HealhGui'.

avatar image Eric5h5 · Sep 15, 2012 at 03:00 AM 0
Share

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.

avatar image AngryCSharpNerd · Sep 15, 2012 at 05:14 PM 0
Share

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.

avatar image Eric5h5 · Sep 15, 2012 at 07:01 PM 0
Share

If you're mixing languages, http://docs.unity3d.com/Documentation/ScriptReference/index.Script_compilation_28Advanced29.html

avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AngryCSharpNerd · Sep 15, 2012 at 05:13 PM 0
Share

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();} }}

avatar image ShiftedClock · Sep 15, 2012 at 07:53 PM 0
Share

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!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Get variable from another object 1 Answer

Getting script from another script bug 1 Answer

I need Help with the bergzerg arcade Melee combat scripts 1 Answer

how to make one script change a variable in another scipt 2 Answers

Any problems with this script? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges