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 BarkShark · Nov 27, 2011 at 05:04 PM · arrayelementsamount

Check amount of elements in array

Hello,

In my script I have 3 options, the arrays contains 1, 2 or 3 elements (colors) Is there a way to check the amount of elements in the array? I don't mean the lenght of the array, I mean the amount of elements in the array. I do this to use it in a switch statement.

   public var colorArray = new Color[3];
     
     Function Update()
     {
     switch(// amount of variables in the array)
     {
     
     case 1 :
     // action 1
     break;
     
     case 2 :
     // action 2
     break;
     
     case 3 :
     // action 3
     break;
 
 }
 }
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
2
Best Answer

Answer by Peter G · Nov 27, 2011 at 05:12 PM

With a value type such as Color, the number of elements should equal the length of an array.

For reference types you can have nulls, but with a value such as Color, there should be as many elements as the length of the array.

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 BarkShark · Nov 27, 2011 at 05:16 PM 0
Share

Ok thanks, but if I declare the lenght from the beginning like I did in this script, the length isn't equal to the amount of elements in the array. Or am I wrong? Thank you for your help

avatar image Peter G · Nov 27, 2011 at 05:32 PM 0
Share

Ok, this is going to annoy you, but it has to do with the serialization. Value types (that aren't specifically marked nullable) can never have a null value. Every element in your array has a value. Its whatever the default is for your value type and in the case of colors, its Color.black.

What's actually happening is Unity simply isn't changing the length of your array like you told it to. This has to do with the public variable serialization. Unity serializes your public arrays so that it can display them in the inspector properly. Unity isn't reading the value you wrote in the script any more, its reading the value its stored from the inspector so it won't change anything if you modify the variables default value in the script.

Two easy ways of fixing it.

  1. $$anonymous$$ake the array private. Unity only serializes public vars for the inspector so private variables are unaffected.

  2. Recreate the array in the start function with the proper length.

avatar image BarkShark · Nov 27, 2011 at 06:04 PM 0
Share

Ok thank you very much, I have figured it out!

avatar image
0

Answer by ptdnet · Nov 27, 2011 at 05:24 PM

 int HowManyElementsAreInMyArray(object[] a) {
   int ohyeahbaby = 0;
   for (x = 0; x < a.GetUpperBounds[0]; x++) {   // might have mispelled that
     if (a[x] != null) ohyeahbaby++;
   }
   return ohyeahbaby;
 }
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 Peter G · Nov 27, 2011 at 05:32 PM 0
Share

For value types this will always return Array.Length.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

getting rid of empty elements in array 2 Answers

Setting arrays equal to each other issue 2 Answers

Using class arrays for multiple Variables per element also Naming Elements 2 Answers

How To Assign List Elements To Array Elements? 2 Answers

Using variables inside a GameObject[] array 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