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 GetUpKidAK · Apr 28, 2013 at 09:49 PM · c#listarrays

Query with C# arrays and List<>

Hi,

Strange question, but I'm new to C# and I'm trying to differentiate between the two blocks of code.

So, this:

 public List<Color>    shapeColor        = new List<Color>();

 if(shapeColor.Count > 0)
         {
             int newColor = Random.Range(0, shapeColor.Count);
             renderer.material.color = shapeColor[newColor];    
         }

And this:

 public Color[]        shapeColor;
 
 if(shapeColor.Length > 0)
         {
             int newColor = Random.Range(0, shapeColor.Length);
             renderer.material.color = shapeColor[newColor];    
         }

Both blocks seem to achieve the same thing - selecting a random colour from an array of colours selected in the inspector. My understanding of C# was that you should use List<> if the array of items you're using is not a set length as it will produce errors otherwise. Here, the colours are picked in the Inspector window, so it can't be defined in the code.

I had initially defined the shapeColor array as below:

 public Color shapeColor[];

But ran into errors with the rest of the code because of the above. My question is, how is using Color[] any different and is could this cause any difficulties?

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

Answer by whydoidoit · Apr 28, 2013 at 09:50 PM

Native arrays "[]" are not resizable at run time. Lists are. You should use native arrays if the contents will not change during the execution of the game and Lists otherwise. They are semantically similar but arrays use Length and Lists use Count for the number of items.

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 GetUpKidAK · Apr 29, 2013 at 11:40 PM 0
Share

Ah! Thanks!

Is there any different between:

 public Color shapeColor[];

and

 public Color[] shapeColor;

$$anonymous$$y previous experience would have lead me to think that the top way was correct in this example, but it seems to be the other way around?

avatar image whydoidoit · Apr 30, 2013 at 06:20 AM 0
Share

No the bottom way is right, though I've been known to find myself typing the top way and having to fix it ;)

The point is you are defining a variable called shapeColor so it comes last. Then the type of that variable is an array of Color so you write that as the type. Hence the bottom one is right.

avatar image
2

Answer by Loius · Apr 28, 2013 at 09:51 PM

[] array is just a bunch of memory in a row. So it's super-fast to access and super-slow to resize (can't be resized, you have to grab a new chunk of memory and copy the values over).

List is a class which essentially contains a [] array and helper functions to make resizing it less prone to error.

There are no errors for using variable-length [] arrays, you just have to be aware that you're using variable-length arrays.

Comment
Add comment · 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

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

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

Related Questions

how to control particles along a path? 0 Answers

Need help with converting an array to a list in C#` 4 Answers

Why is everything being set the same? Please Help! 1 Answer

Array of list not working when building or running on platforms? 0 Answers

How do i convert this code snippet to a list? 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