- Home /
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];
}
}
You should definitely consider using a List of cards. Lists are perfectly suited to replace just about any array which needs dynamic resizing.
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));
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().
@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.
@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.
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
![](https://koobas.hobune.stream/wayback/20220613170819im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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