Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Destroying objects between scenes?

Discussion in 'Getting Started' started by yummynath, Jul 21, 2020.

  1. yummynath

    yummynath

    Joined:
    Jul 3, 2020
    Posts:
    19
    Hello! Obligatory : I'm a noob at Unity and coding. I'm trying to create a 2D top down RPG which has the player going on "quests", consuming items and beating baddies. The regular.

    I have 2 scenes right now, an Overworld and a small path that leads to an Orchard scene. I used Scene Management and a triggered box collider to change scenes and that works perfectly. In said Orchard there are few item pick ups such as Health/Ammo etc. Once I collect it, go back into the Overworld scene, and if I get back to the Orchard scene, those items reappear and I would like to destroy them permanently.

    I've read online that Don'tDestroyOnLoad will preserve the current state of the object, but given there will be quite a few around the game, I think it's going to create a lot of "clutter" in the memory. I could also create a boolean value for that pick up and have it checked on the Awake/Start function, but again I would think there's a more efficient way of doing it (since I plan to have around 30 items around the world in general, I know I'll be prone to error). I've tried "SetActive" to false, created a bool value that check if the setactive is false and called it on the Start function, but I'm sure that doesn't work since the scene reloads all information fresh.

    Is there any function or code I can use to say "Permanently" destroy the game object once picked up, and if I head back to scene it'll go away?
     
  2. basnijhof01

    basnijhof01

    Joined:
    May 8, 2020
    Posts:
    3
    Okay you should have a questmanager that keeps check of which 'quests' you have completed. Then every time you enter the Orchard scene there should be a function that checks whether you've completed such quest, if so, deactivate the items.
     
  3. yummynath

    yummynath

    Joined:
    Jul 3, 2020
    Posts:
    19
    Thank you so much for your answer! I think the Quest Manager makes absolute sense for specific quest related items. I'm wondering about non-quest related general items such as an "Ammo pickup". For now the Orchard has a gameobject that successfully refills the ammo amount by a specific number, and I'd like that to disappear so that players don't farm it and go back and forth between scenes to refill their ammo, well unfairly.

    Therefore once the Player leaves the Orchard, or for that matter any other scene (example another I've made is a house where there is an Ammo and Health pick up), I'd like that particular object to be destroyed upon reloading the scene. Would I have to go through the quest manager for this? Or could I add something to the script these items have that does not reload upon pickup and re-entry into the scene?

    So far, this is what I've written.

    private void OnTriggerStay2D(Collider2D other)
    {
    if(other.CompareTag("Player") && isArrowPickup)
    {
    Debug.Log("Collect!");
    Bow.instance.ArrowPickUp(amount);
    Destroy(gameObject);
    }
    if(other.CompareTag("Player") && isHealthPotion)
    {
    Debug.Log("Add Health");
    PlayerHealthController.instance.HealPlayer(amount);
    Destroy(gameObject);
    }
    }