Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Mathias · Aug 01, 2011 at 04:51 PM · getcomponentawake

GetComponent(); in Awake cant be accessed in Update

Hi.

I dont know if I am doing something wrong but, even with scripts not written by me I cant use a variable(var) using GetComponent from a different function than I am referring it from(Does that make sense)? I will give you an example:

 function Awake (){
 var controller : CharacterController = GetComponent(CharacterController); //Do it in awake instead of Update to preserve performance.
 }
 
 function Update (){
 if(controller.isGrounded){
     print("We are grounded");
     } 
 }

But it will say "Unknown identifier ´controller´" Or something like that.

But if I use GetComponent in Update it will compile just fine. But for performance reasons I want to do it in awake.

So am I doing anything wrong?

Thank you. :)

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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Joshua · Aug 01, 2011 at 05:00 PM

Variables only exist in there own ´scope´. Scope is a block of code, create by { }. Variables declared outside of any function live in the class, which in JS is the entire script. Variables declared inside a function only live inside that function.

This is extremely basic stuff. If you don't know this I advice you to read a book for follow a few guides.

 var globalVar : boolean;
 
 function Start()
 {
     var inFunctionVar: boolean;
 }
 
 function Update()
 {
     //globalVar can be accessed here, inFunctionVardoes not exist here.
 }
Comment
Add comment · Show 1 · 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 Mathias · Aug 01, 2011 at 05:09 PM 0
Share

Ha I did not know that. Thanks.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Getcomponent returns null on ios 1 Answer

GetComponentInChildren returns null during Awake() 1 Answer

StateMachineBehaviour get component on awake ?? 1 Answer

Why do I get a context error when using GameObject.FindWithTag in Awake/Start function and GetComponent in a function? 1 Answer

get_enabled can only be called from the main thread. 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