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

Make food items reappear after it was eaten?

Discussion in 'Scripting' started by vivianindatub, May 7, 2021.

  1. vivianindatub

    vivianindatub

    Joined:
    Apr 23, 2021
    Posts:
    71
    How do you guys make items reappear at certain times after it has been destroyed?
    I want to generate random food objects in a scene and have player eat them. After a while the food objects will respawn back in the scene so player can find them and eat again.
    What's the best way to approach this?
    Should I use scriptable objects or prefabs for this? (I have looked up several tutorials but still can't quite wrap my head around the concepts of prefabs and scriptable objects)
     
  2. Ngakk

    Ngakk

    Joined:
    Jul 11, 2019
    Posts:
    7
    You can start by having a script that's in charge of spawning the food and getting notified when that spawned food has been eaten, then you can just call unity's Invoke function to delay the respawn.

    The spawner would need a reference to the object to be spawned in the form of a prefab, and the prefab would need a reference to the spawner (that reference's set on spawn) so it can notify when it's been eaten.

    Then you would put one spawner for each food item you want to be respawned on the scene.
     
    Kurt-Dekker likes this.
  3. MDADigital

    MDADigital

    Joined:
    Apr 18, 2020
    Posts:
    2,198
    For perfomace I would do it with a single component and a bunch of spawn points
     
  4. rubcc95

    rubcc95

    Joined:
    Dec 27, 2019
    Posts:
    222
    I would do it depending on the food I want to spawn. If you eat a carrot, will a carrot always appear again? Or a cupcake may appear. If it will be the same, I would just disable the carrot and enable it after few moments (object pooling), else I would use MDADigital solution.
     
  5. MDADigital

    MDADigital

    Joined:
    Apr 18, 2020
    Posts:
    2,198
    Why wouldnt that work with my approach? Just have transform fro each spawnpoint. It can have a monob that points out stuff like which items are possible to spawn on that point etc.
     
  6. rubcc95

    rubcc95

    Joined:
    Dec 27, 2019
    Posts:
    222
    When I said you approach wouldn't work? I just clarify that it could be done differently if it is always the same object, not that it can't be done just with a single MonoBehavior, moreover object polling is used to be done with a single manager instance.
     
    Last edited: May 7, 2021