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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
1
Question by AlotaFahjina · Nov 05, 2014 at 10:32 PM · c#arraynewbie

Deleting an Element from an Array.

Firstly Im very new at this, I've messed with scripting in the past on a small scale, So im no pro at c#.

So Anyways,

I have an array that I have stored a Deck of Cards in. When a new card is created it randomly picks a element from the deck of cards and creates the card.

I guess my first question I need to ask, Is when removing an element from an array how does it reorganize the array. Say the array has a length of 10, If I delete depth 5 of the array, does the rest of the array shrink automatically,as In will the element 6 become 5 and so on. If it doesnt how do I reorganize the array so i dont have a empty element.

Second part of my question is after it creates the card I want to delete that particaluar element of the array so that card can not be created again. how do I delete from an array.

Here is creating my card from the array i want to delete the element of the array that i get from the int drawCard :

     newCard.gameObject.GetComponent<SpriteRenderer>().sprite = arrayOfPairs[drawCard].cardSprites[0];

Here is my whole code :

 using UnityEngine;
 using System.Collections;
 
 public class testing : MonoBehaviour {
 
 
     public GameObject blankCard;
 
     public MyPairs[] arrayOfPairs;
 
     public int cardimage;
 
     int drawCard;
 
     int handSize = 0;
 
     int NewCard = 0;

     int deckSize = 9;
 
 
     // Use this for initialization
     void Start () {
 
         while(handSize < 9)
         {
 
             drawCards();
 
             handSize++;
 
         }
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 
     void drawCards(){
 
         GameObject newCard = (GameObject)Instantiate(blankCard);
 
         newCard.name = ("newCard" + NewCard);
 
         NewCard++;

             drawCard = Random.Range (0, deckSize);
 
         newCard.gameObject.GetComponent<SpriteRenderer>().sprite = arrayOfPairs[drawCard].cardSprites[0];
 
 
     }
 
 }
 


Comment
Add comment · Show 1
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 AlwaysSunny · Nov 05, 2014 at 09:58 PM 1
Share

You should definitely consider using a List of cards. Lists are perfectly suited to replace just about any array which needs dynamic resizing.

1 Reply

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

Answer by Kiwasi · Nov 05, 2014 at 10:41 PM

As indicated the collection structure you really want is a generic List.

Here are some of the calls you should consider using. There are plenty of others, check out the documentation above.

 //At the top of your script
 using System.Collections.Generic;

 // Initialisation
 List<Card> cardsInDeck = new List<Card>();

 // Adding cards
 cardsInDeck.Add(new Card());

 //Removing a random card
 int index = Random.Range(0,myCard.Count);
 Card myCard = cardsInDeck[index];
 cardsInDeck.RemoveAt(index));


Comment
Add comment · Show 4 · 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 Unitraxx · Nov 06, 2014 at 12:36 AM 1
Share

It's better to use the randomly generated index in combination with RemoveAt() for the removal. Since this is an O(1) operation in contrast to the O(N) operation of Remove().

avatar image Kiwasi · Nov 06, 2014 at 02:40 AM 1
Share

@Unitraxx that is somewhat true. RemoveAt() is faster as there is no searching of the data for the item. I'll update my answer to show this.

However RemoveAt() is still an O(n) operation as the elements after the removed item need to be renumbered. The only remove operation in a generic list that is truly O(1) is removing the last element on the list.

Check out the documentation on $$anonymous$$SDN for more details.

avatar image Landern · Nov 06, 2014 at 02:55 AM 1
Share

@Bored$$anonymous$$ormon, You're right, List(source code for List class) is backed by an array of T, if you want to get super special and away from O(n), .net does offer the LinkedList which is great for pre-pending among other reasons can offer better performance in certain circumstances. The List object after a certain size(85$$anonymous$$B(ish)) gets allocated to the LOH(Large Object Heap) which introduces another layer of complexity. But i'm rambling.

avatar image AlotaFahjina · Nov 06, 2014 at 03:26 AM 0
Share

It took me a bit to figure out how to use that, but sweet. I kept having na$$anonymous$$g issues of the List, Cause I was trying to name the list of something that didnt exist before i figured out I could put the name of my class in there. Im still learning and this just made my day.

Thank you very much.

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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Spell-casting combo system 1 Answer

Checking bool on multiple script instances. 1 Answer

Set default length for an array of elements of a custom class in inspector 0 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