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
1
Question by UBayir · Feb 16, 2016 at 12:22 PM · variablevariableslocalglobal

Global or local variables for use at start function just once

I have a game that use variables only when loading scene.I use all of them for just once. I suppose to think that local variables are running on RAM and the global ones on the harddrive. Am I wrong? Should I create these variables global or local .. which is the best way? Is this effecting apk or ipa size? I want to minimalize the size of my game I make mobile games.

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

Answer by Teravisor · Feb 16, 2016 at 01:42 PM

Strictly speaking, variables do affect size of apk by several bytes as compiled code changes. Hovewer, even in mobile systems that's nothing, so consider that they don't affect size. Trying to optimize that will bring you to no result.

To minimalize size of game, you should make sure Unity doesn't add any unused libraries inside, compress textures/sounds/etc as much as you can. Next, remove all references to unused assets and make sure there are no unused assets in Resource folder - they are added to package if they are referenced or in Resource folder. Using procedural generation instead of pre-built assets is slow loading-performance-wise, but can reduce size of .apk as well. And that's pretty much it - scripts are very small, trimming them for size of .apk is useless.

About other questions. First you're mixing definitions:

  • Local variables are variables that are defined inside some scope (usually method/class scope). Most often "local variables" refer to method scope variables while variables in class are called something like "class variables".

  • Global variables are variables available from anywhere in code. In C# that's singletone variables (=marked with static).

All variable descriptions, default values and how they change are initially stored in binary compiled code(inside .apk/.ipa there is file with it) and when application starts they are all created in RAM so no accesses to disk to write/read variables happen after initial binary loading. Don't mix variables up with assets themselves - assets are loaded from disk only when they are referenced when scene is loaded or there is call to method that loads them like Resources.Load. Local variables are removed from RAM when their scope disappears (method ends/class is destroyed by garbage collector), global variables don't disappear until program ends.

If you're bothered with cluttering RAM with class variables you won't use, add them to some Component that does something with them on Start() and then gets Destroy(...) after they are all used so that RAM is cleared afterwards.

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 UBayir · Nov 13, 2016 at 11:13 PM 0
Share

Thanks for your answer ..)

avatar image
1

Answer by meat5000 · Feb 16, 2016 at 01:45 PM

If your variable is only going to be used once and then is rendered obsolete it makes sense to declare it within your method so that it becomes destroyed when you are done using it, thus getting back your memory. Declaring Globally will make the variable exist for the lifetime of the script which is not very efficient usage of RAM.

For a few variables its not really important but it is a practice you want to be familiar with, especially if you undertake a large project.

On mobile devices RAM management is REALLY IMPORTANT as mobile OS have a habit of culling processes to recuperate resources. If your Game becomes a strain on the system it may terminate the app.

Dont stress about small projects and micromanagement but DO practice good technique.

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 UBayir · Dec 10, 2016 at 09:56 AM 0
Share

I got it Thanks man :))

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

Global Variables Refuse to Cooperate 1 Answer

Should I create local copy of global variable? 3 Answers

Editing local variables in another script 2 Answers

Accessing variables from seperate scripts 1 Answer

How to access this variable? 2 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