Home > Net >  Unity - List in Component Doesn't Reset to Default After Exiting Play Mode
Unity - List in Component Doesn't Reset to Default After Exiting Play Mode

Time:01-15

So I have a deckbuilder I'm making, and I have a function to add a card to the active deck.

public void OnPointerClick(PointerEventData eventData)
        {
            List<CardConfig> cards = gameManager.GetActiveDeck();
            foreach (CardConfig currentCard in cards)
            {
                Debug.Log(currentCard);
            }


            Debug.Log("ADDING CARD");
            //Get gameobject's card config
            CardConfig card = gameObject.GetComponent<InventoryCardDisplay>().GetCardConfig();
            gameManager.AddCardToDeck(card);

            cards = gameManager.GetActiveDeck();
            foreach (CardConfig currentCard in cards)
            {
                Debug.Log(currentCard);
            }

            //Call Populate Deck
            //pauseMenu.PopulateActiveDeck(gameManager.GetActiveDeck());
        }

This is the function that captures a click event and adds the card to the player's active deck which is housed on a game manager prefab.

This all seems to work on face value, the deck shows the card that has been added and the debug after adding the card shows that the card has been added.

The problem is that after the card has been added and I exit play mode I would expect the deck to reset to what has been defaulted in the inspector. But when I play again and add another card the deck the debug before adding the card shows the deck with the card I added in the previous play.

Meaning that the list persists between plays for some reason and it doesn't make sense to me.

I'm not sure what to try as this hasn't happened to me before and I was under the impression that the components would reset between plays.

For context I'm on Unity 2021.3.16f1.

CodePudding user response:

I fixed it but I don't know why this is the way it is.

I had a serializefield variable where I could drag and drop the game manager right in and it would save the list changes to the main prefab. But once I changed that variable to a FindObjectOfType<DeckManager>() the problem was fixed.

I don't like using those functions as they are expensive but it seems to be the only way I could get it to work.

  • Related