Can't remove item from list?

I have a segment of code that emulates drawing a card and removing it from the deck scriptable object. It used to work as intended but after I removed the script and put it back, it is no longer removing the card from the deck. Any ideas?

void Update () {
        if (Input.GetKeyDown (KeyCode.N)) {
            //check for deck out
            if(playerDeck.deckCard.Count == 0){
                Debug.Log("No more cards in the deck!");


            }
            else{
                int deckMax = playerDeck.deckCard.Count;
                string cardImage;
                int rndSelector;
                rndSelector = Random.Range (0,deckMax);
                cardImage = playerDeck.deckCard[rndSelector].card_img;
               
               
                //Create GameObject
                GameObject go = new GameObject ("cardManifest" + cardcount);
                SpriteRenderer renderer = go.AddComponent<SpriteRenderer> ();
                go.AddComponent<BoxCollider2D> ();
                go.AddComponent<hand_mouseover>();


                //Render Sprite
                Sprite cardSprites = Resources.Load<Sprite>("Images/" + cardImage);
                renderer.sprite = cardSprites;

                Debug.Log ("Drew " + playerDeck.deckCard[rndSelector].card_name);
                //Transform and Distribute
                go.transform.localScale = new Vector3 (cardScale, cardScale, cardScale);

                //go.transform.localPosition = new Vector3 (DistPos, 0.5f, 0f);
               
                cardcount = cardcount + 1;
               
                //distrubte cards with method
                distributeCards ();

                //add to hand...
                Scriptable_CardObject drawnCard = playerDeck.deckCard[rndSelector];
                playerHand.Add (drawnCard);
                infoText.text= "Drew " + drawnCard.card_name;

                //Remove card drawn from the deck array
                playerDeck.deckCard.RemoveAt (rndSelector);

            }
        }
    }

Thanks!

This code looks like it should work as intended. Show you class members and any error output from console if you have any.

    public Sprite sprite;
    public int cardcount = 0; //total cards drawn
    public float cardScale = 0.1f; //rendered sprite scale

    public Text infoText;
    public List<Scriptable_CardObject> playerHand = new List<Scriptable_CardObject> ();
    public Scriptable_Deck playerDeck;
    public CardDatabase cardDatabase;

That’s what I thought! No errors at run time! Everything is linked up in the inspector and the cards draw as intended. They just aren’t being removed.

Can you show your Scriptable_Deck code? I thought that was a list but it’s apparently not.

using UnityEngine;
using System.Collections;
#if UNITY_EDITOR
using UnityEditor;
#endif
using System.Collections.Generic;

[System.Serializable]
public class Scriptable_Deck : ScriptableObject {
    public string deckName;
    public List <Scriptable_CardObject> deckCard = new List<Scriptable_CardObject>();
}

The issue seems to have sorted itself. For some reason the scene mucked up. Thanks anyway!

I have this same issue, how exactly did you fix it?

That thread ended nine years ago. Make your own thread. BE SPECIFIC about what YOUR issue is, because I doubt your issue is the same.

1 Like

Closing because search exists.