How do i remove a list item from the scene while adding the next?

I’m making a flashcard game and want to add a flashcard from a list to the scene with a button press. I can add the cards to the scene but sometimes they end up on the same layer as the previous flashcard and i can’t see it. I would like it if each time i add a new flashcard from the list it ends up on the next layer (infront of the previous card) so i can see it, but i think it would be better to completely remove the previous flashcard when i press the button to add the next. How do i do that?

Here is what i have so far.

public class ListManager : MonoBehaviour
{
public List characterPrefabs = new List();

// Start is called before the first frame update
void Start()
{
    
}

// Update is called once per frame
void Update()
{
    if (Input.GetKeyDown(KeyCode.RightArrow))
    {
        if (characterPrefabs.Count > 0)
        {
            int characterIndex = Random.Range(0, characterPrefabs.Count);

            Instantiate(characterPrefabs[characterIndex], new Vector3(0, 0, 0), characterPrefabs[characterIndex].transform.rotation);

            characterPrefabs.RemoveAt(characterIndex);
        }

        if (characterPrefabs.Count == 0)
        {
            Debug.Log("Game Over");
        }
    }
}

}

Hiya! You can simply store the most recently shown object as a variable, then destroy it when a new object is shown. I wrote some changes to your script that might help with that.


        List<GameObject> characterPrefabs = new List<GameObject>();

        // Added a variable to store the value of our last
        // shown object!
        private GameObject lastShownObject;

        void Start()
        {
            
        }

        void Update()
        {
            if (Input.GetKeyDown(KeyCode.RightArrow))
            {
                if (characterPrefabs.Count > 0)
                {
                    // If the last shown object exists, destroy it.
                    // Alternatively, if you just want to keep it around,
                    // but deactivate it, you could use lastShownObject.SetActive(false)
                    if (lastShownObject)
                        Destroy(lastShownObject);

                    int characterIndex = Random.Range(0, characterPrefabs.Count);

                    // Store this new object as the last shown object
                    lastShownObject = Instantiate(characterPrefabs[characterIndex], new Vector3(0, 0, 0), characterPrefabs[characterIndex].transform.rotation);

                    characterPrefabs.RemoveAt(characterIndex);
                }
                if (characterPrefabs.Count == 0)
                {
                    Debug.Log("Game Over");
                }
            }
        }

In short, Instantiating an object returns that newly-created object, so you can store it to a variable. Then, you can simply check if it exists and destroy it before instantiating a new one (which is then stored to the variable in its place).