Author Topic: NGUITools.DestroyImmediate() problem  (Read 5613 times)

tokola

  • Guest
NGUITools.DestroyImmediate() problem
« on: July 03, 2012, 12:53:32 PM »
Hi there.

I am currently facing a problem with NGUITools.DestroyImmediate(). I have a UITable being filled with prefabs (label & sprite combination) using NGUITools.AddChild(obj, prefab). When trying to destroy the objects and repopulate the table using a for loop, a weird thing happen. The first time it works fine, the second time one item is left behind and end up with a list n+1 (n=list count), and the third time two objects resist destruction and the table becomes n+2! Thereafter, it persists with 2 superfluous items no matter how many more times I run the code.
The code is as simple as this:
  1. foreach (Transform calib in m_calibrationTable.transform)
  2.         {
  3.         NGUITools.DestroyImmediate(calib.gameObject);
  4.         }
I also tried NGUITools.Destroy() but doesn't destroy the objects at all. Any ideas why this is happening?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: NGUITools.DestroyImmediate() problem
« Reply #1 on: July 03, 2012, 02:17:47 PM »
Well yeah, you are destroying objects from a list you are looping through. Think about it. The list you're looping through gets modified with every destroy call. How do you expect it to work?

Try the inverse for loop:

  1. for (int i = 0, imax = m_calibrationTable.childCount; i > 0; )
  2. {
  3.     Transform calib = m_calibrationTable.GetChild(--i);
  4. }

tokola

  • Guest
Re: NGUITools.DestroyImmediate() problem
« Reply #2 on: July 03, 2012, 03:34:46 PM »
Thanks for the response. However, now it doesn't destroy the objects at all! They keep adding up every time the script is called.
  1. for (int i = 0, imax = m_calibrationTable.transform.childCount; i > 0; )
  2. {
  3.     Transform calib = m_calibrationTable.transform.GetChild(--i);
  4.     NGUITools.DestroyImmediate(calib.gameObject);
  5. }

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: NGUITools.DestroyImmediate() problem
« Reply #3 on: July 03, 2012, 07:13:54 PM »
Why are you using DestroyImmediate anyway? It shouldn't be used at run-time. It's pretty much an editor-only function, same as Object.DestroyImmediate.

Just use Destroy instead.

tokola

  • Guest
Re: NGUITools.DestroyImmediate() problem
« Reply #4 on: July 05, 2012, 05:00:34 PM »
I have initially tried using Destroy, but it leaves empty lines at the beginning of my table. E.g. if I have 4 items in the list, the items appear from the 5th row on and the first 4 rows are blank! I found that DestroyImmediate resolves the problem, without a clear reason.

I also fixed the loop by using the following code, and it works fine now:
  1. for (int i = m_calibrationTable.transform.childCount-1; i >= 0; --i)
Thanks

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: NGUITools.DestroyImmediate() problem
« Reply #5 on: July 05, 2012, 06:27:33 PM »
Unparent the object before destroying it, and you'll fix that.

Unity doesn't destroy anything until the end of the frame. Doesn't unparent things either.