前提・実現したいこと
以前の質問で、子オブジェクトをすべてDestroyする際、
親オブジェクトにnullを代入するとより確実に不具合を防げることをご教示いただいたのですが、
Transform.DetachChildrenというメソッドを知ったので、
これに書き換えても問題ないかご教示お願い致します。
書き換えて問題ないならば、個人的にはTransform.DetachChildrenを使うほうがキレイなコードに見えるので、
そちらを使いたいと思います。
該当のソースコード
下記2つのソースコードは同等ですか?
C#
1if(this.transform.childCount > 0){ 2 this.transform.Cast<Transform>().ToList().ForEach(x => { x.parent = null; GameObject.Destroy(x.gameObject);}); 3}
C#
1if(this.transform.childCount > 0){ 2 this.transform.Cast<Transform>().ToList().ForEach(x => GameObject.Destroy(x.gameObject)); 3 this.transform.DetachChildren(); 4}
試したこと
this.transform.DetachChildren()の後に、
Debug.Log(transform.childCount);をとって、
0だったので、おそらく、同等かと思うのですが、
念の為、質問させていただきました。
あなたの回答
tips
プレビュー