Author Topic: Destroying objects with Panels  (Read 4219 times)

Olle

  • Guest
Destroying objects with Panels
« on: February 04, 2013, 06:09:44 AM »
I see this is a common error, but I found no real answers to my problem anywhere so I am bold an pop the question once again -

Basically I have put panels with a label on all my items, which always face the player and scale accordingly. It looks cool and works nicely :) However, when I pick up an item I ofcourse will want to destroy it instead of leaving it hanging. But this gives several problems.

1. Scene is being destroyed while there are hidden renderers that are using it. This is likely an editor script creating objects with hideFlags and leaving them enabled.
- This seems to be "solved" by rebooting unity, however, it is really annoying you have to do that aaaaaall the time O_o .. also everytime I start up with this error there is at least one panel hanging in the air, but it does not exists in the hiearchy, which is... weird :S

2. Destroying object immediately is not permitted during physics trigger and contact callbacks. You must use Destroy instead.
- This is annoying.. I am using an OnTriggerEvent() I've tried calling Destroy(other.gameObject); gives the error, tried NGUITools.Destroy(other.gameObject); even tried all combos of both destroy functions with finding the children first and deleting them first like this:
          
                NGUITools.Destroy(other.transform.FindChild("panel").transform.FindChild("Label").gameObject); //Destroys label
      NGUITools.Destroy(other.transform.FindChild("panel").gameObject);//Destroy The panel first
      Destroy (other.gameObject); //Then destroy the item

What is even more weird, that this did work for like five seconds then I tried starting up again, and then it didnt work O_o ... Also when I do any number of these combos, there will be a label/panel hanging in the air in its last transform, but it is deleted in the heirachy... How the deuce do I destroy them panels? :O

Please somebody help me with this :)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Destroying objects with Panels
« Reply #1 on: February 04, 2013, 09:16:18 AM »
2. Don't disable/destroy anything in OnTriggerEnter. That's physics. You can only do it in the main thread. Do this instead:
  1. void OnTriggerEnter() { StartCoroutine(DelayedDisable()); }
  2.  
  3. IEnumerator DelayedDisable() { <whatever you did in the OnTriggerEnter> }

1. Doing #2 properly will likely make #1 go away. If not, make sure that your scripts don't have [ExecuteInEditMode] on them.

Olle

  • Guest
Re: Destroying objects with Panels
« Reply #2 on: February 04, 2013, 02:36:20 PM »
Didn't work :(

You can see my code here:

  1. //Function that checks if the player collides with items
  2.         void OnTriggerEnter(Collider other){ StartCoroutine(DelayedDisable(other)); }
  3.        
  4.         IEnumerator DelayedDisable(Collider other){
  5.                 if(other.gameObject.layer == 10){
  6.                                 //switch case controlling what is being collided with
  7.                         switch(other.gameObject.name){
  8.                         case "metal":
  9.                                 print("this is metal");
  10.                                 break;
  11.                         }      
  12.                 }
  13.                 //Destroy(other.transform.FindChild("panel").transform.FindChild("Label").gameObject);
  14.                 //Destroy(other.transform.FindChild("panel").gameObject);//Destroy The panel first
  15.                 Destroy (other.gameObject); //Then destroy the item
  16.                 yield return null;
  17.         }

I tried again with all combos of NGUI.Destroy() and Destroy() but still no luck >_< I made a small video so you can see what it looks like http://www.justin.tv/projectolle/b/364018260

redhawk

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 34
    • View Profile
Re: Destroying objects with Panels
« Reply #3 on: February 04, 2013, 02:38:08 PM »
Shouldn't you be sending GameObject other instead of Collider other?  I am definitely no expert at coding, so mainly just a thought

Olle

  • Guest
Re: Destroying objects with Panels
« Reply #4 on: February 04, 2013, 02:42:10 PM »
Quote
Shouldn't you be sending GameObject other instead of Collider other?  I am definitely no expert at coding, so mainly just a thought

Would most certainly be more clean to do it that way, but this was done quickly just to see if it works ;)

However, I can see if I destroy the panels and labels on the object they are placed (from a script on the object) there is no error, so perhaps I could make some function which destroys the panels in that script and call it through the first one... gotta try and fiddle a little with that in the meanwhile
« Last Edit: February 04, 2013, 02:43:55 PM by Olle »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Destroying objects with Panels
« Reply #5 on: February 04, 2013, 07:40:20 PM »
Your "yield return null;" is in the wrong place. You need to "yield return new WaitForEndOfFrame();", and then destroy.

Olle

  • Guest
Re: Destroying objects with Panels
« Reply #6 on: February 05, 2013, 05:48:27 AM »
Your "yield return null;" is in the wrong place. You need to "yield return new WaitForEndOfFrame();", and then destroy.

Works! :D Thanks a mill Aren, you've been a great help ^_^