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 mdagreda · Jan 11, 2013 at 08:32 PM · arrayfunctionvariables

How do I access a variables created in a function with another function?

I have created a multiple arrays in a function that I would like to accress from another function. For instance I have created a function called getStuff and in it I create several arrays and will them with objects or strings or some numeric varibles. Point is a few different types of arrays. Once the function is complete I want to keep the arrays around until I press a button then I want the arrays to be destroyed and new arrays made with new variables to fill them. Any ideas or question?

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 AndyMartin458 · Jan 11, 2013 at 09:57 PM 0
Share

In C# all variables that are declared inside of a function are marked for garbage collection when that function call ends. When you declare a class variable, it is accessible within that class. Your best bet is to access these variables and set their content inside of functions. C++ allows for static function member variables that can be useful for monitoring how many times a function has been called, but this is not possible in C#.

int var;
int var = 0; //initializes the variable to 0
private int var; //same as int var
protected int var; //can be accessed by itself and child classes
public int var; //can be accessed by any class in the project

1 Reply

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

Answer by Lovrenc · Jan 11, 2013 at 08:55 PM

If you do this:

 void myFunct() {
   int myVar = 5;
 }

You CAN NOT ACCESS that myVar ANYWHERE outside that function. You could however do this:

In your class, declare needed variables:

 int myVar;
 float[] myArray;

In your function you use those variables to save the data you need.

 void DoSomething() {
   //DO CALCULATIONS
   myVar = 5;
   myArray = new float[]{1f, .5f, .1f};
 }


Then when you press a button you just call the DoSomething function. It will create new values by itself and store them in those variables.

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 mdagreda · Jan 11, 2013 at 09:57 PM 0
Share

Thanks, I actualy just copy the arrays I made in the functions into some global arrays. So that is pretty much what you showed. Thanks again

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

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Array Element number 1 Answer

Find gameObject with higher variable 1 Answer

Variable value doesn't change 1 Answer

changing variables values !! 1 Answer

Create new variable name based on array index 3 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