• 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 ThaGameMaker467 · Jan 01, 2013 at 04:36 PM · arrays

For Loops, While Loops, and Arrays

I am not that new to Unity but i do not know when i need to use arrays and the different loops. i can use if statements thoes are very simple but if anyone can tell me the proper time to use an array that will be very helpful. Thanks.

Comment
Add comment · Show 2
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 MarkFinn · Jan 01, 2013 at 04:52 PM 0
Share

Normally someone would be along shortly to explain that this question is far to general for the Unity Answers Site (which is intended for pretty specific questions), and point you to the Unity forums which are more open to general questions, but this question is rather too general even for there. It's a real old school "How long is a piece of string?" style.

Heck, if someone asked you "When should I use a piece of string?" (with no context whatsoever) you'd be quite within reason to think they must be somewhat insane. Same deal here.

avatar image ThaGameMaker467 · Jan 01, 2013 at 04:54 PM 0
Share

Ok lol sorry about that i guess i will search online to find out more and ask a more specific question.

1 Reply

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

Answer by MaGuSware™ · Jan 01, 2013 at 04:48 PM

Well, the proper time to use an array is when you have a collection of data of the same type. For example, you want to generate 20 numbers and store them... you wouldn't want to have 20 individual variables so you use an array (size 20).

C#

 //[] = array. so int[] means int array.
 int[] numbers = new int[20];

You then access 'elements' within the area using the key value, starting at 0. So a size 20 array will have 0-19 as the references.

So to flesh out the example of random generation (using a for loop):

C#

 int[] Generate(int count, int min, int max)
 {
     int[] numbers = new int[count];

     //Start at 0, and make sure the iterator (i) does not go beyond the array limit 
     for (int i = 0; i < count; i++)
     {
         numbers[i] = Random.Range(min,max);
     }

     return numbers;
 }

For loops should be used when you know how many times the loop will need to process. It is appropriate here because we know that 'count' will be the amount of times it needs to run.

While loops should be used when you have no idea how many times it would need to run. Like when you are making something procedural and you are using a boolean to check to see if it is done or not.

Comment
Add comment · Show 2 · 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 ThaGameMaker467 · Jan 01, 2013 at 04:56 PM 0
Share

Thanks although ill have to do a little more research this really helps.

avatar image clunk47 · Jan 01, 2013 at 06:57 PM 1
Share

Very nice answer, consdiering the question being so vague lol. +1

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

11 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 avatar image

Related Questions

Multiple Cars not working 1 Answer

my script not working..!!! 1 Answer

Unity Jet Wing Animations! Help! 1 Answer

Cursor Lock to Center of screen script doesnt work please help. 2 Answers

Script Errors! 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