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
0
Question by kidne · Aug 18, 2015 at 11:41 AM · arrayclassdot net

Array Reordering?

I cannot use a List. I have a set amount of Elements in an array, don't need to add or remove any- just need to be able to switch the Elements to different positions in the Array.

For example, I have a check that looks for an empty array. If the array behind it (index i + 1 ) DOES have a value, then the array that has something in it will replace the one that has nothing.

I also would like to be able to reorder based on an Element's variable (int) and this needs to be done in an Update() function of some kind.

Here is an example of my code I'm working on.

friendsList is a custom class Array. friendLabel is a GameObject[] with UI elements in the children.

 for(int i = 0; i < friendsList.Length; i++)
             if (friendsList.ElementAt (i).frienduser != null)
         {
             friendLabel[i].SetActive(true);
             friendLabel[i].GetComponentInChildren<Text>().text = friendsList.ElementAt (i).frienduser + " \n *" 
                 + friendsList.ElementAt(i).friendStatusMessage;
 
             // turn their state into the corresponding sprite imge!
             friendLabel[i].GetComponentInChildren<Image>().sprite = statuses[friendsList.ElementAt (i).state];
         }
         else //frienduser is null
         {
             friendLabel[i].SetActive(false);
 
             if (friendsList.ElementAt (i + 1).frienduser != null) 
             {
                 //if we have something behind this slot that is not empty, replace this empty one with the full one!
                 //friendsList.Equals(friendsList.ElementAt (i + 1); // = friendsList.ElementAt (i + 1); Doesn't work. :  ^  (
             }
         }
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 brunopava · Aug 18, 2015 at 12:01 PM 0
Share

There is a library that helps reordering arrays/lists.

Its called System.Linq

avatar image mateiasu · Aug 18, 2015 at 12:12 PM 0
Share

If you don't want to use any managed or utilized arrays / lists / etc (incl Generics 'n Linq) then you need to implement all by your own.

The last line does not work because at the end of the array i + 1 exceeds the array's length and thus is out of scope. You should add a check

 if (i + 1 < friendsList.Length)

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Instantiating gameobjects in an array / class problem | NullReferenceException: 0 Answers

JSON missing arrays 1 Answer

How to access a variable within a multidimensional array of class 2 Answers

Problem with Singleton and NullReferenceException? 0 Answers

Need my function to work with different lists of different values (classes) 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