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 DarkKobra2020 · Apr 03, 2012 at 09:31 AM · variablesothercallingfrom

Calling variables from other scripts

Ok I have followed every single question and I even looked at the manual and this is getting ridiculous. I keep trying to call my variables curHealth and maxHealth for a GUI script from the Health script

var health : Health = GetComponent(Health);

function OnGUI() { GUI.Label(Rect(10,10,100,20), "Health:" + health.curHealth + "/" + health.maxHealth); }

@script RequireComponent (Health)

I either get errors that say that variables can not be called using the GetComponent function but if I do it the way it says in the manual it will just say it needs a reference.

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
1
Best Answer

Answer by Kryptos · Apr 03, 2012 at 09:37 AM

You cannot get the component upon the declaration of the variable (it might not be ready at that time). What you need to do is retrieve the component during the Awake phase:

 @script RequireComponent (Health)
 
 // declaration
 var health : Health;

 // retrieve component
 function Awake()
 {
     health = GetComponent(Health);
 }

 // use it
 function OnGUI()
 {
     GUI.Label(Rect(10,10,100,20), "Health:" + health.curHealth + "/" + health.maxHealth);
 }
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 DarkKobra2020 · Apr 03, 2012 at 10:31 AM 0
Share

Thank you so much maybe now I can continue my RPG :)

avatar image DarkKobra2020 · Apr 03, 2012 at 10:35 AM 0
Share

Ok in normal curcumstances you would be correct but I think there is something wrong with my version of Unity as its now giving an Argument exception.

ArgumentException: You are not allowed to call GetComponent when declaring a variable. $$anonymous$$ove it to the line after without a variable declaration. Don't use this function in the constructor or field initializers, ins$$anonymous$$d move initialization code to the Awake or Start function. HUD..ctor () (at Assets/Scripts/Player/HUD.js:1)

All I did was copy your code to see if it would work.

avatar image
0

Answer by TheLedworks · Apr 03, 2012 at 09:38 AM

hp = GetComponent("Health") as Health; //do this at initialization

void OnGUI() {

GUI.Label(Rect(10,10,100,20), "Health:" + hp.curHealth + "/" + hp.maxHealth);

}

Comment
Add comment · 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How do you return variables from other scripts? 2 Answers

Access other script from Editor script 2 Answers

NullReferenceException : Object reference not set to an instance of an object 0 Answers

Get valor of variable in another script 1 Answer

Issue calling a variable from another script - C# 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