CarolinaVideos
CarolinaVideos

Reputation: 1

How to destroy instanciated game object and creat it again?

I'm making a solitaire game and I strugle with one functionality.

In time of player is clicking on deck cards appears in discard pile. They are created in factory :

[SerializeField]private GameObject prefab;

    public void Create(Transform parent, float zOffset, float yOffset, CardModel model, bool isCovered, bool isLast, string pileName, GameObject curretPile, ActionsManager manager)
    {
        GameObject instance = Instantiate(prefab, new Vector3(parent.position.x, parent.position.y - yOffset, parent.position.z - zOffset), Quaternion.identity, parent);
        this.model = model;
        this.view = instance.GetComponent<CardView>();
        this.controller = GetComponent<CardController>();
        instance.GetComponent<CardController>().SetModel(this.model);
        instance.GetComponent<CardController>().SetView(view);

        string spriteName = (instance.GetComponent<CardController>().ReturnValue()).ToString() + instance.GetComponent<CardController>().ReturnSuit();
        Sprite faceingSprite = uncoverSprite.Find(item => item.name == spriteName);
        instance.GetComponent<CardController>().SetUncoveredSprite(faceingSprite);

        instance.GetComponent<CardController>().SetPileName(pileName);
        instance.GetComponent<CardController>().SetIsCoveredBool(isCovered);
        instance.GetComponent<CardController>().SetIsLastInPile(isLast);
        instance.GetComponent<CardController>().SetActionsManager(manager);
        instance.GetComponent<CardController>().SetCurrentPile(curretPile);

When all cards are created to discard pile and player click again on deck it sends action with demand of the list of card from discard pile, they are returning to deck and they are destroy in discard pile.

public void OnCardDemand()
    {
        List<CardModel> list = ReturnList();
        RemoveAllElementsFromList();

        if(OnDeckSend != null)
        {
            OnDeckSend(list);
        }

CardController[] cards = gameObject.GetComponentsInChildren<CardController>();

        foreach(CardController card in cards)
        {
            Destroy(card.gameObject);
        }

    }

Cards should be instatiated again after clicking on deck pile but there is an error : MissingReferenceException: The object of type 'SpriteRenderer' has been destroyed but you are still trying to access it.

How can I destroy cards without any error?

Upvotes: 0

Views: 71

Answers (1)

Chuck
Chuck

Reputation: 2102

It's a deck of cards - I wouldn't try to continuously create and destroy them, just assign new transform parents and set local positions to zero if you want to move them. I'd also recommend using SetActive(false) instead of destroying, then you can use SetActive(true) to "create" them back - this is along the lines of an object pool.

Ultimately the problem is that you've cached a reference to a Component on the GameObject that you're deleting, Wherever you're using the SpriteRenderer you need a null check, or else you need a more sophisticated way to unsubscribe that reference in OnDestroy for the GameObject your destroying.

Upvotes: 1

Related Questions