Remove game object from list

Hi, how can i remove instantiaed gameobject from list?
I found how to remove index from list now i just need to remove object at that index.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SpawnMoney : MonoBehaviour
{
private List<GameObject> spawnedMoney = new List<GameObject>();
public GameObject moneyP;


void TestSpawn()
    {
        spawnedMoney.Add(Instantiate(moneyP, spPoint.position, Quaternion.identity));
        spPoint.position += new Vector3(0 + 2, 0, 0);
        current++;
        if (current == howMuch)
        {
            isdone = true;
            CancelInvoke("TestSpawn");
        }
        if (current == row)
        {
            spPoint.position += new Vector3(0 - 10, 0, 0 + y);
            current = 0;
        }
    }

public void StartRemoving()
    {
        PlayerMovement playerRef = thisIsplayer.gameObject.GetComponent<PlayerMovement>();
        if (playerRef)
            if (!playerRef.isFull)
            {
                playerRef.AddMoneyToPlayer();
                spawnedMoney.RemoveAt(spawnedMoney.Count - 1);
              

                Debug.Log(spawnedMoney.Count);
            }
    }



}

Hi,

Do you mean remove the gameobject from the scene, like destroying it?
Try this:

// Get a reference to the game object, before it’s removed from the list.
GameObject toDestroy = spawnedMoney[spawnedMoney.Count -1)];

// Then destroy it
Destroy(toDestroy);

1 Like

If i do this than it does not remove starting from last.
i instantiate 5 items and it starts to remove from 4.

spawnedMoney.RemoveAt(spawnedMoney.Count - 1);
GameObject ToDestroy = spawnedMoney[spawnedMoney.Count - 1];
Destroy(ToDestroy);

Ehhm, you may want to re-read what you just wrote, go through your code step by step in your head and think about what you’re doing. Your current code removes the last object from the list. After that you grab the now last object and destroy it, while keeping the reference to that destroyed object in the list… You may want to re-order your code to

GameObject ToDestroy = spawnedMoney[spawnedMoney.Count - 1];
Destroy(ToDestroy);
spawnedMoney.RemoveAt(spawnedMoney.Count - 1);

So you grab the last element in the list, you destroy it and then you remove the last element from the list.

1 Like

thank you for info.
This is what got me in List there apparently is separate index list and object list. I come from UE4 Bp’s and there you can remove index at or just item.

Huu? I’m not sure what you’re talking about. A generic list in C# does not have a seperate “index list”. It’s just an ordinary array wrapped in a class with a counter, nothing more (well a bit boilerplate but essentially that’s it). Here’s a reference implementation of the class.

Maybe you are confused about “removing an element from a list” and “destroying a gameobject”. Those are two completely seperate things to begin with. The List class also has a Remove method which can remove an element based on the element value. However that’s less efficient since that method has to first find the provided value inside the list and then remove the element at that index. So when you know the index using RemoveAt is way faster, especially when it’s the last element.

When you put a gameobject reference into a list, all you do is storing a reference to the gameobject in that list. The list is not responsible or in any way related to the lifetime of the gameobject. The gameobject lives in the scene. You can completely get rid of the List and the gameobjects would still be there unless you destroy them. Removing an element from a list will completely remove it from that list, just like when you remove the name from the list of people you want to invite to your birthday party, that doesn’t kill that person or make it vanish. The name simply is no longer in the list.