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
1
Question by robinking · Aug 05, 2010 at 11:19 AM · variablepublicscopedeclaration

Can I declare a variable inside a function which has instance scope?

I have a prefab containing a script that has several member arrays (builtin arrays). Another script creates each instance of this prefab and sizes the arrays differently.

At present, I am declaring the variables at the top of the script, then calling the initialization function from the other script, which gives it the size of its array:

private var things; private var thingSize : int[];

function MakeThings(numberPassedFromOtherScript) { things = numberPassedFromOtherScript; thingSize = new int[things]; for (a=0;a<things;a++) { thingSize[a] = Random.value*10; } }

Is there a way to instead declare the variables inside that MakeThings() function, sizing them at the same time, and still have access to the variables and arrays after MakeThings() ends? Something like this... (I know I'm using global incorrectly here, this is just for illustration.)

function MakeThings(numberPassedFromOtherScript) { global var things = numberPassedFromOtherScript;

global var thingSize = new int[things]; for (a=0;a < things; a++) { thingSize[a] = Random.value*10; } }

Comment
Add comment · Show 1
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 jashan · Aug 05, 2010 at 11:31 AM 0
Share

What would be the advantage of doing it that way? As far as I can see, it would only complicate things and make the code harder to maintain ... which is probably why this isn't possible (at least not in any language I would program in ;-) ).

1 Reply

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

Answer by jashan · Aug 05, 2010 at 11:30 AM

No, obviously, scope is defined by the declaration - so if you declare a variable inside a method, it has method scope. What would be the use of having member variables declared in a method anyways, except for making things complicated and hard to understand? ;-)

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 robinking · Aug 05, 2010 at 11:34 AM 1
Share

Thanks! I kinda thought so. Sometimes one has to ask stupid questions in order to learn ;-)

avatar image Mike 3 · Aug 05, 2010 at 11:41 AM 1
Share

Jashan - unfortunately, you actually can declare a global scope variable inside a function inside normal javascript (Which makes things less obvious). I expect that's where some of the confusion comes from

avatar image jashan · Aug 06, 2010 at 04:57 AM 0
Share

Hi $$anonymous$$ike, ah ... yeah ... "regular JavaScript". I wasn't aware that this is possible but I can totally understand how this is confusing. Actually, that's one of the reasons why I usually write UnityScript - to make the distinction because it's really quite confusing to have a language named "JavaScript" which is so different from browser JavaScript. And, yeah, I can imagine that browser JavaScript supports such oddities ;-)

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

What is the difference between 'public var' and 'var' when declaring variables? 3 Answers

Why can't I to declare public the variable "Clave"? 1 Answer

Cannot access AudioSource from within function 0 Answers

Declaring List variable for subclasses with baseclass or without type? 1 Answer

Find + GetComponent not working as expected 0 Answers


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