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 FlashX · Sep 30, 2015 at 10:57 AM · arrayremove

Remove element from array

hi dudes!

I'm trying to remove an element from an array, though the array I'm using has a different syntax to the one i can find that I found in the manual.

how can i remove an element from this?

 Color[] colors = {Color.green,Color.red, Color.white, Color.blue, Color.yellow, Color.black}; 
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 MerryAce123 · Sep 30, 2015 at 11:16 AM 0
Share

Arrays have predefined size so you cannot simply add or remove slots. The easiest way is to find an index of your element that you want to remove and then assign null value to it. Like this:

 int index = 0; // any nuber you need
 colors[index] = null; // removes an element from the "index slot"

 
avatar image Bunny83 MerryAce123 · Sep 30, 2015 at 11:31 AM 0
Share

Since Color is a value type you can't set it to "null". Even when it was a reference type (or nullable type) the element is not "removed" since it's still there but with the value of null.

3 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by Bunny83 · Sep 30, 2015 at 11:29 AM

Like others have already mentioned, arrays have a fix size. Once created the size can't be "changed". However it's possible to "resize" an array, but it actually creates a new array and copies all values over.

See this SO answer:

 public static void RemoveAt<T>(ref T[] arr, int index)
 {
     for (int a = index; a < arr.Length - 1; a++)
     {
         // moving elements downwards, to fill the gap at [index]
         arr[a] = arr[a + 1];
     }
     // finally, let's decrement Array's size by one
     Array.Resize(ref arr, arr.Length - 1);
 }

 RemoveAt(ref colors, 2); // removes Color.white.

Using a generic List is recommended since it only creates a new array if necessary. So if you add / remove a lot elements a List will be better in speed and "garbage production".

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 FlashX · Oct 03, 2015 at 07:00 AM 0
Share

Thanks guys! I'm gonna come back to this and try and get my head round it so don't think that ive forgotten, I'm thinking i should tidy up some of my code before i get to it :)

avatar image Wellmann · Feb 01, 2016 at 09:48 AM 3
Share

If the order of the array does not matter you can also do just:

 public static void RemoveAt<T>(ref T[] arr, int index)
  {
       // replace the element at index with the last element
      arr[index] = arr[arr.Length - 1];
      // finally, let's decrement Array's size by one
      Array.Resize(ref arr, arr.Length - 1);
  }
avatar image guinoalaskapp · Jun 21, 2021 at 05:48 PM 0
Share

Array does not exist in the current context :v

That is the error Unity show me with this code

avatar image
1

Answer by Denvery · Sep 30, 2015 at 11:06 AM

You can't remove the element from this array in lite, normal, intuitive ways. Please use List<Color> for enabling removing tool

https://msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.110).aspx

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

Answer by ravenclarico · Jun 21, 2021 at 08:35 PM

Most people will tell you to use Lists instead, however that's a cop-out and doesnt really answer the question (but they're probably right) if you really need to remove an element from an array, and you can't use lists or any other libraries, I'm pretty sure the only way is to create a new array and replace the old one with it. below is an example of a function that you could use to do that

 int[] removeAtIndex(int[] inputArray, int index){
  int[] outputArray= new int[inputArray.length-1];
  for(int i=0;i<index;i++){
   outputArray[i]=inputArray[i];
  }
  int writeLoc=index;
  int readLoc=index+1;
  for(int writeLoc=index;writeLoc<outputArray.Length;writeLoc++){
   outputArray[writeLoc]=inputArray[readLoc];
   readloc++;
  }
  return outputArray;
 }

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

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

Related Questions

array question 2 Answers

how to delet used array item 2 Answers

How do i get my script to work? 2 Answers

C# array equal to another array minus one entry. 1 Answer

how to add list after removing some 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