Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Destroy Last Cloned Object of Scene when pressing "Z"

Discussion in 'Scripting' started by KidCanada7, Jun 19, 2022.

  1. KidCanada7

    KidCanada7

    Joined:
    Jul 19, 2018
    Posts:
    13
    I'm trying to make it so if you want to Undo the last spawned in Object with my spawn menu it would Destroy it by pressing "Z". But I don't know how to implement this, is it possible through some List of some sort? So every Cloned/Spawned in item goes directly to a list and then when you undo it will remove/destroy the newest from the list?
     
  2. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,404
    A stack would be ideal for this. Its like a list, but in a specific order. You can push objects onto it and when you pop it the last added item will be returned and removed.
    Every time you clone you add to the stack, every z you pop the last item off the stack and destroy it
     
    KidCanada7 likes this.
  3. KidCanada7

    KidCanada7

    Joined:
    Jul 19, 2018
    Posts:
    13
    I'm gonna try this, thanks!
     
    DevDunk likes this.