Hello Guest

Author Topic: DestroyImmidiate  (Read 10086 times)

Therg

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 2
    • View Profile
DestroyImmidiate
« on: November 25, 2013, 09:42:25 pm »
Hey I have been started to get an error message since I updated to unity 4.3. I get the error message several times when I enter play mode/exit play mode. The error message is:

Destroying object multiple times. Don't use DestroyImmediate on the same object in OnDisable or OnDestroy.
UnityEngine.Object:DestroyImmediate(Object)
tk2dUtil:DestroyImmediate(Object) (at Assets/TK2DROOT/tk2d/Code/tk2dUtil.cs:50)
tk2dRuntime.TileMap.SpriteChunk:DestroyColliderData(tk2dTileMap) (at Assets/TK2DROOT/tk2dTileMap/Code/tk2dTileMapChunks.cs:68)
tk2dRuntime.TileMap.Layer:DestroyGameData(tk2dTileMap) (at Assets/TK2DROOT/tk2dTileMap/Code/tk2dTileMapChunks.cs:404)
tk2dTileMap:OnDestroy() (at Assets/TK2DROOT/tk2dTileMap/Code/tk2dTileMap.cs:146)

The code looks like this:

public static void DestroyImmediate( UnityEngine.Object obj ) {
      if (obj == null) {
         return;
      }

      #if UNITY_EDITOR && !(UNITY_3_5 || UNITY_4_0 || UNITY_4_0_1 || UNITY_4_1 || UNITY_4_2)
            if (!Application.isPlaying && undoEnabled) {
               UnityEditor.Undo.DestroyObjectImmediate(obj);
            }
            else
      #endif
      {
         UnityEngine.Object.DestroyImmediate(obj);
      }
   }

Do you have any suggestion about what I can do to get rid of the error message?

Thanks //Oscar

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: DestroyImmidiate
« Reply #1 on: November 25, 2013, 11:00:28 pm »
What version of tk2d are you using? I have tested in tk2d 2.3.0 and I don't seem to be able to reproduce this. Is there anything special in your setup?

Therg

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: DestroyImmidiate
« Reply #2 on: November 26, 2013, 07:04:14 pm »
I am using 2d toolkit 2.3.0 and unity 4.3.04f. I don't think there is anything special with my setup.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: DestroyImmidiate
« Reply #3 on: November 26, 2013, 11:03:24 pm »
Can you try to reproduce this with the tile map demo? It will probably help narrow down why this is happening.

McTomas

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: DestroyImmidiate
« Reply #4 on: February 03, 2014, 04:50:38 pm »
Hi,

I’m having the same issue.

In my case I have 2 unity scenes (a menu and a game scene). I have the a map created with tile map on my menu scene and every time at runtime I go from the menu to the game I get this red warning on Unity console multiple times:

Destroying object multiple times. Don't use DestroyImmediate on the same object in OnDisable or OnDestroy.
UnityEngine.Object:DestroyImmediate(Object)
tk2dUtil:DestroyImmediate(Object) (at Assets/TK2DROOT/tk2d/Code/tk2dUtil.cs:50)
tk2dTileMap:OnDestroy() (at Assets/TK2DROOT/tk2dTileMap/Code/tk2dTileMap.cs:150)

On the Tilemap demo this doesn’t happen because there is only one scene and you don’t switch from that demo scene to a new scene.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: DestroyImmidiate
« Reply #5 on: February 04, 2014, 10:34:51 am »
I'm trying to reproduce this with the demo scene by duplicating and calling Application.LoadLevel - I can't seem to reproduce this. Can you try and this a really simple test case? I think I may be missing something fundamental here.

McTomas

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: DestroyImmidiate
« Reply #6 on: February 05, 2014, 12:17:27 am »
Ok. Just noticed that in the middle of the entire red warning I had this ones

Destroying object multiple times. Don't use DestroyImmediate on the same object in OnDisable or OnDestroy.
UnityEngine.Object:DestroyImmediate(Object)
tk2dUtil:DestroyImmediate(Object) (at Assets/TK2DROOT/tk2d/Code/tk2dUtil.cs:50)
tk2dRuntime.TileMap.SpriteChunk:DestroyGameData(tk2dTileMap) (at Assets/TK2DROOT/tk2dTileMap/Code/tk2dTileMapChunks.cs:54)
tk2dRuntime.TileMap.Layer:DestroyGameData(tk2dTileMap) (at Assets/TK2DROOT/tk2dTileMap/Code/tk2dTileMapChunks.cs:405)
tk2dTileMap:OnDestroy() (at Assets/TK2DROOT/tk2dTileMap/Code/tk2dTileMap.cs:146)

Another thing is I do LoadLevelAsync() and the tile map is a pre saved prefab. The prefab is automatically destroyed by Unity on scene change.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: DestroyImmidiate
« Reply #7 on: February 05, 2014, 10:36:00 am »
Is this the same case where the tile map render data object was attached to the tile map object?

McTomas

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: DestroyImmidiate
« Reply #8 on: February 05, 2014, 01:59:05 pm »
Nop, but the tile map and the render data objects have the same parent in the prefab where they are included

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: DestroyImmidiate
« Reply #9 on: February 05, 2014, 02:09:28 pm »
That could explain it. The render data is created & destroyed dynamically if it doesn't exist. It doesn't need to be in the prefab, in fact it would create a lot more instances if it were in a prefab. Try removing that and it should work a lot better.

McTomas

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: DestroyImmidiate
« Reply #10 on: February 05, 2014, 04:06:45 pm »
Ok, that fixed everything.

But now I have another problem. Most of my code needed to have the render data next to the tile map because I’m moving the parent object around (because it’s literally a map).

How can I achieve that  If the tiles are created dynamically?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: DestroyImmidiate
« Reply #11 on: February 05, 2014, 04:09:46 pm »
One way to do that is to attach a script to your tile map that will mirror the transforms on the data object. Or, detach it before unloading.

McTomas

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: DestroyImmidiate
« Reply #12 on: February 05, 2014, 05:28:35 pm »
Thanks, that did it :)