2D Toolkit Forum
2D Toolkit => Support => Topic started by: Therg 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
-
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?
-
I am using 2d toolkit 2.3.0 and unity 4.3.04f. I don't think there is anything special with my setup.
-
Can you try to reproduce this with the tile map demo? It will probably help narrow down why this is happening.
-
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.
-
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.
-
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.
-
Is this the same case where the tile map render data object was attached to the tile map object?
-
Nop, but the tile map and the render data objects have the same parent in the prefab where they are included
-
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.
-
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?
-
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.
-
Thanks, that did it :)