Help us understand the problem. What is going on with this article?

UnityのDestroyは即時反映されないので、DestroyImmediateを使う

More than 3 years have passed since last update.

GameObject を Destroy() して、ヒエラルキー上からは消えても、実はしっかり存在している。
GetComponents で要素検索するとふつうにカウントされて返ってくるので、知らないとはまる(はまった)。

ちなみに、対象に null を入れても、GetComponents で取得できてしまう。

Debug.Log (GetComponents<Transform>().Length); // 1
Destroy (targetObject);
targetObject = null;
Debug.Log (GetComponents<Transform>().Length); // 1

(GCに時間かかってる・・?)

で、そういうときは、 DestroyImmediate を使うと即時破棄される。
めでたしめでたしかと思ったが、DestroyImmediate は推奨されてないっぽい。

http://docs.unity3d.com/ja/current/ScriptReference/Object.DestroyImmediate.html

直ちにオブジェクトを破壊する。ですが、Destroy 関数の方を使うことを推奨します

うーん、 コルーチンで非同期処理にすればよい・・のか?
子要素の全削除なら、Detach してやるって方法もある。

foreach (Transform n in transform)
{
    Destroy(n.gameObject);
}
transform.DetachChildren ();
nenjiru
Creative Technologist https://twitter.com/nenjiru http://www.slideshare.net/minorunakanou
http://nenji.ru
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした