Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Can't remove item from list?

Discussion in 'Scripting' started by laxbrookes, Apr 20, 2015.

Thread Status:
Not open for further replies.
  1. laxbrookes

    laxbrookes

    Joined:
    Jan 9, 2015
    Posts:
    235
    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?

    Code (CSharp):
    1. void Update () {
    2.         if (Input.GetKeyDown (KeyCode.N)) {
    3.             //check for deck out
    4.             if(playerDeck.deckCard.Count == 0){
    5.                 Debug.Log("No more cards in the deck!");
    6.  
    7.  
    8.             }
    9.             else{
    10.                 int deckMax = playerDeck.deckCard.Count;
    11.                 string cardImage;
    12.                 int rndSelector;
    13.                 rndSelector = Random.Range (0,deckMax);
    14.                 cardImage = playerDeck.deckCard[rndSelector].card_img;
    15.                
    16.                
    17.                 //Create GameObject
    18.                 GameObject go = new GameObject ("cardManifest" + cardcount);
    19.                 SpriteRenderer renderer = go.AddComponent<SpriteRenderer> ();
    20.                 go.AddComponent<BoxCollider2D> ();
    21.                 go.AddComponent<hand_mouseover>();
    22.  
    23.  
    24.                 //Render Sprite
    25.                 Sprite cardSprites = Resources.Load<Sprite>("Images/" + cardImage);
    26.                 renderer.sprite = cardSprites;
    27.  
    28.                 Debug.Log ("Drew " + playerDeck.deckCard[rndSelector].card_name);
    29.                 //Transform and Distribute
    30.                 go.transform.localScale = new Vector3 (cardScale, cardScale, cardScale);
    31.  
    32.                 //go.transform.localPosition = new Vector3 (DistPos, 0.5f, 0f);
    33.                
    34.                 cardcount = cardcount + 1;
    35.                
    36.                 //distrubte cards with method
    37.                 distributeCards ();
    38.  
    39.                 //add to hand...
    40.                 Scriptable_CardObject drawnCard = playerDeck.deckCard[rndSelector];
    41.                 playerHand.Add (drawnCard);
    42.                 infoText.text= "Drew " + drawnCard.card_name;
    43.  
    44.                 //Remove card drawn from the deck array
    45.                 playerDeck.deckCard.RemoveAt (rndSelector);
    46.  
    47.             }
    48.         }
    49.     }
    Thanks!
     
  2. jtsmith1287

    jtsmith1287

    Joined:
    Aug 3, 2014
    Posts:
    787
    This code looks like it should work as intended. Show you class members and any error output from console if you have any.
     
  3. laxbrookes

    laxbrookes

    Joined:
    Jan 9, 2015
    Posts:
    235
    Code (CSharp):
    1.     public Sprite sprite;
    2.     public int cardcount = 0; //total cards drawn
    3.     public float cardScale = 0.1f; //rendered sprite scale
    4.  
    5.     public Text infoText;
    6.     public List<Scriptable_CardObject> playerHand = new List<Scriptable_CardObject> ();
    7.     public Scriptable_Deck playerDeck;
    8.     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.
     
  4. jtsmith1287

    jtsmith1287

    Joined:
    Aug 3, 2014
    Posts:
    787
    Can you show your Scriptable_Deck code? I thought that was a list but it's apparently not.
     
  5. laxbrookes

    laxbrookes

    Joined:
    Jan 9, 2015
    Posts:
    235
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. #if UNITY_EDITOR
    4. using UnityEditor;
    5. #endif
    6. using System.Collections.Generic;
    7.  
    8. [System.Serializable]
    9. public class Scriptable_Deck : ScriptableObject {
    10.     public string deckName;
    11.     public List <Scriptable_CardObject> deckCard = new List<Scriptable_CardObject>();
    12. }
     
  6. laxbrookes

    laxbrookes

    Joined:
    Jan 9, 2015
    Posts:
    235
    The issue seems to have sorted itself. For some reason the scene mucked up. Thanks anyway!
     
  7. samuelchanceholden

    samuelchanceholden

    Joined:
    Mar 29, 2023
    Posts:
    2
    I have this same issue, how exactly did you fix it?
     
  8. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,367
    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.
     
    Bunny83 likes this.
  9. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,042
    Closing because search exists.
     
Thread Status:
Not open for further replies.