LoginSignup
5
5

More than 5 years have passed since last update.

UIWrapContentの罠

Posted at

問題

タブの切り替えでGameObjectをDestroy()して再作成したら、以下のようなエラーが出てしまった。

MissingReferenceException: The object of type 'Transform' has been destroyed but you are still trying to access it.
UnityEngine.Transform.get_localPosition ()
UIWrapContent.WrapContent () (at Assets/NGUI/Scripts/Interaction/UIWrapContent.cs:157)

同じ現象

解消法

Destroy()するたびにそのGameObjectをSetActive(false)にし、GameObjectを作られた後にWrapContentをStart()する

仮コード(適当)

bool destroyed = false;
foreach (Transform child in Grid.gameObject.transform)
{
    if (xxx) // Destroy()が走ったら
    {
        child.gameObject.SetActive(false); // まず無効にする
        destroyed = true;
        Destroy(child.gameObject);
    }
}

...

UIWrapContent wrapContent = UIGrid.GetComponent<UIWrapContent>();
int maxVisibleCount = wrapContent.maxVisibleCount;
wrapContent.maxCount = targetDataModel.Length;
wrapContent.onInitializeItem = this.OnInitializeScrollItem;

...

foreach (TargetDataModel data in targetDataModel)
{
    ...
    GameObject go = CreateScrollItem();
    ...
    go.SetActive(true);
}

if (destroyed) 
{
    wrapContent.Start(); // ここでWrapContentを再起動
}
5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5