Search Unity

Instantiate a pickup or activate?

Discussion in 'Scripting' started by DustyShinigami, Nov 3, 2018.

  1. DustyShinigami

    DustyShinigami

    Joined:
    Jan 5, 2018
    Posts:
    529
    I currently have some pickups that destroy once collected. Is destroying them better in terms of performance or is it just as fine to deactivate them? The latter seems easier when I want them to reactivate. Otherwise, if destroying and reinstantiating them is better, I’m not sure how to do this properly, despite having achieved it for the player. Though that was through watching a tutorial.

    I currently have a coroutine in my health manager for when the player dies, but considering I already have a gold pickup script, I could do with having my instantiate function in there for the coroutine to reference. I can’t seem to get it to work though. I’ve tried setting up a start position variable and a respawn variable (one Vector 3, the other Quaternion) and a game object called goldBar, but every combination I’ve tried doesn’t do anything.

    I want it so when the player fails to activate a checkpoint and dies, the gold resets.
     
  2. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Enabling/Disabling an object is more performant than instantiating/destroying an object because there is no garbage collection involved. The downside being you would then be using more memory since the object still exists in the scene.

    But generally, this only matters if you have objects being instantiated/destroyed many times per second, such as in a bullet hell game, where that would dramatically affect performance.

    In your case, it seems like either option would be fine, as it doesn't sound like you'd have the player constantly picking up and dropping items.
     
    Last edited: Nov 4, 2018
    DustyShinigami likes this.
  3. DustyShinigami

    DustyShinigami

    Joined:
    Jan 5, 2018
    Posts:
    529
    Cool. Thanks for the explanation. That makes my current situation easier. I have noticed though, despite only having a small scene with little going on, that each time I pick up an object, it gets destroyed and a little particle effect takes place, there’s always a momentary stutter.