Oops! He did it again

He had us do the DetachChildren call again but future him did not stop and tell us to NOT do that. You can even still see in the hierarchy all of the placeholder UI prefabs still there :joy:

This seems to work for me. It is similar to what we did in DialogueUI. However, there we had a reference to the root GO in the script. This just looks for children andā€¦ murders them:

    private void Start()
    {
        // Clean up placeholder quests.
        foreach (Transform kiddo in GetComponentInChildren<Transform>())
        {
            Destroy(kiddo.gameObject);
        }

        foreach (Quest quest in tempQuests)
        {
            QuestItemUI uiInstance = Instantiate<QuestItemUI>(questPrefab, transform);
            uiInstance.Setup(quest);
        }
    }
1 Like

Yes, FutureSam (well, now ā€œLess In The Past Than The Original Samā€) only mentions this bug once, but it does occur here as well. Looks like youā€™re on top of it, though. Good job spotting the issue here as well.

This might be a great opportunity to use an Extension method.

public static class TransformExtensions {
    public static void DestroyChildren(this Transform transform) {
        for (var i = transform.childCount - 1; i >= 0; i--) {
            Object.Destroy(transform.GetChild(i).gameObject);
        }
    }


    public static void Reset(this Transform transform) {
        transform.position = Vector3.zero;
        transform.localRotation = Quaternion.identity;
        transform.localScale = Vector3.one;
    }
}

This way you can just call transform.DestroyChildren() anywhere you need to.

3 Likes

Privacy & Terms