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 schwertfisch · Jan 15, 2011 at 09:06 PM · variablefunctionvarothersame

In a function, how do I access a variable of another function in the SAME script? (js)

I have a script, with 2 functions. These 2 functions are called at different moments, by another script on another gameObject.

The first function, creates a variable (it's the length of an array that this 1st function creates at runtime using "GameObject.FindGameObjectsWithTag").

I want to use this variable that the 1st function creates in the 2nd function, but when I put it in the 2nd function like this:

while (varName >=1) { ...

I get the error:

"varName" is not a member of 'function ():void'

What should I do then, to use in the 2nd function, the variable that the 1st function creates?

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

Answer by Mike 3 · Jan 15, 2011 at 09:12 PM

You need to declare the variable outside of the functions. That way, it's in the scope of the class, which lets you use it from both functions

i.e.

var test : int;

function a() { test = 4; }

function b() { Debug.Log(test); }

Comment
Add comment · Show 3 · 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 schwertfisch · Jan 15, 2011 at 09:20 PM 0
Share

Thanks a lot $$anonymous$$ike, I thought that since the var's value (which is the length of an array created at runtime, as I said) should be defined through the 1st function, I should also set the var itself in that function. What I did now, was setting the variable at the beginning of the script, without giving it a value. The 1st function does that and I can use it in the 2nd function. O$$anonymous$$, maybe my question was a very silly, but it seemed really confusing just before you answered...

avatar image Mike 3 · Jan 15, 2011 at 09:25 PM 0
Share

The problem is that a variable declared in a function only exists while that function is running. I have a feeling web javascript handles it differently, but that's another issue entirely

avatar image schwertfisch · Jan 16, 2011 at 09:52 AM 0
Share

Got it- thanks again $$anonymous$$ike ;-)

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

No one has followed this question yet.

Related Questions

variable doesn't change 1 Answer

How do I reassign the result of a function back to the original arguments? 3 Answers

What is this C# code in javascript 0 Answers

Variable Doesn't Update in "Update" function 0 Answers

How can I storing the IEnumerator with parameters? 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