unity3d - C# Unity Engine destroy() subclass -
hello have code:
public abstract class test : monobehaviour { public abstract void onstart(); public abstract void onupdate(); private void start() { onstart(); } private void update() { if (input.getkeydown(keycode.x)) { destroy(this); } onupdate() } } then have this:
public class secondtest : test { public override void onstart() { } public override void onupdate() { writter.log("running"); } } however whenever press x, writter keeps logging, seems instance of "test" gets destroyed not "secondtest" there way solve this? thank you!
*op explained intention cause children destroyed when parent is.
- the solution better handled @ '(test)manager' or 'levelmanager'.
you assign children or prefabs (test)manager manually, or (test)manager instantiate child prefab.
gameobject child = instantiate(prefab); child.transform.setparent(gameobject.transform); //set child's parent managerthen in (test)manager
private void update() { if (input.getkeydown(keycode.x)) { for(int i=0;i<gameobject.transform.childcount;i++) destroy(gameobject.transform.getchild(i).gameobject); } }
this crude suggestion, there better ways such holding references children in manager versus parenting.
Comments
Post a Comment