c# - Destroy a deactivated object -


i want destroy deactivated instance of quad prefab (hp bar) , able destroy activated ones :

private gameobject correspondinghpbar;  private string correspondinghpbarname;  void start()  {      correspondinghpbarname = "hpbar1"  }  void update() {     correspondinghpbar = gameobject.find (correspondinghpbarname);      if (shiphp <= 0)     {         destroy (correspondinghpbar);          destroy (gameobject);      } } 

this doesn't work deactivated objects, googled hard failed find answer.

deactivated object don't have start or update method called (nor coroutine matter). in fact when object deactivated own time frozen.

what create method destruction , find way call script (for example kind of controller keeps reference hp bars in scene).

the following pseudo-code (didn't check if compiles, should adapt anyway):

// in script hp bar public boolean trydestroy() {     if (shiphp <= 0)     {         destroy (correspondinghpbar);          destroy (gameobject);          return true;     }     return false; }  // in script private list<hpbar> _allhpbars; void awake() {     _allhpbars = new list<hpbar>(findobjectsoftype(typeof(hpbar))); }  void update() {     var destroyedhpbars = new list<hpbar>();     foreach (var hpbar in _allhpbars)     {         if (hpbar.trydestroy())         {             destroyedhpbars .add(hpbar);         }     }     foreach (var destroyedbar in destroyedhpbars)     {         _allhpbars.remove(destroyedbar);     } } 

Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -