Hello Guest

Author Topic: Destroying child of gameObject but spritecollection remains in hierarchy  (Read 2872 times)

rc

  • Newbie
  • *
  • Posts: 1
    • View Profile
Hi,
I'm pretty new to Unity and tk2d and I have the following problem:

'Onclick' I want to replace a sprite in a placeholder gameObject. Everything works, the view gets updated, the child gets destroyed, but I'm worried about the spritecollection of that child that remains in my view hierarchy. Won't this cause memory issues? Can I destory the spritecollection too or will it automatically be garbage collected?

This is what I do:

Code: [Select]
// remove all previously added children
var children = new List<GameObject>();
foreach (Transform child in placeholder.transform) children.Add(child.gameObject);
children.ForEach(child => Destroy(child));

// adding new child


string extension = Path.GetExtension(path);
if (extension != null && extension.Length > 0)
path = path.Substring(0, path.Length - 4);

Texture2D texture = (Texture2D) Resources.Load(path);
if (texture == null)
throw new Exception("can't find resource for path " + path);

tk2dSpriteCollectionSize size = tk2dSpriteCollectionSize.Explicit(Constants.ORTHO_SIZE, Constants.TARGET_HEIGHT);
Rect region = new Rect(0, 0, texture.width, texture.height);
Vector2 anchor = new Vector2(region.width / 2, region.height / 2);

GameObject newChild =  tk2dBaseSprite.CreateFromTexture<tk2dSprite>(texture, size, region, anchor);
newChild.transform.parent = placeholder.transform;
newChild.transform.localPosition = new Vector2(0,0);

Thanks in advance for any advice.
« Last Edit: October 30, 2013, 12:14:18 pm by rc »