2D Toolkit Forum

2D Toolkit => Support => Topic started by: vsn3e8 on May 20, 2013, 09:06:13 pm

Title: Strange bug, destroying gameobject destroys ALL colliders
Post by: vsn3e8 on May 20, 2013, 09:06:13 pm
I'm running into a strange bug where by destroying a single instance of a game object with a tk2dsprite seems to screw up or destroy all colliders of every other instance of that same sprite.

edit:

Dug around a little, appears to be caused by the destruction of the shared mesh
Title: Re: Strange bug, destroying gameobject destroys ALL colliders
Post by: unikronsoftware on May 21, 2013, 08:24:54 am
I presume you're talking about mesh colliders here?
Also, can you pelase give me more info to investigate this with:
1. Unit version
2. 2D Toolkit version
3. How you're creating the sprite (instantiate, I guess?)



Title: Re: Strange bug, destroying gameobject destroys ALL colliders
Post by: vsn3e8 on May 21, 2013, 06:05:34 pm
unity ver 4.1.2f
2d ver 1.92 final

I first run through and create caches of all collection data.
All sprites are then instantiated off of cached version's which are cached by a unique identifier.



I presume you're talking about mesh colliders here?
Also, can you pelase give me more info to investigate this with:
1. Unit version
2. 2D Toolkit version
3. How you're creating the sprite (instantiate, I guess?)
Title: Re: Strange bug, destroying gameobject destroys ALL colliders
Post by: unikronsoftware on May 21, 2013, 07:20:58 pm
How do you spawn the sprite itself?
Title: Re: Strange bug, destroying gameobject destroys ALL colliders
Post by: vsn3e8 on May 22, 2013, 08:08:28 pm
GameObject.Instantiate(m_cachedSprites[spriteId]);



How do you spawn the sprite itself?
Title: Re: Strange bug, destroying gameobject destroys ALL colliders
Post by: unikronsoftware on May 23, 2013, 01:52:02 pm
I've managed to reproduce this, and I know what it is exactly. Just not sure how to "fix" this without making it take a lot longer to initialize. I can give you a tmp fix if you want. Email support at unikronsoftware dot com and I'll give you a script to work around this.
Title: Re: Strange bug, destroying gameobject destroys ALL colliders
Post by: vsn3e8 on May 23, 2013, 10:15:38 pm
Sure will do.

The workaround I have currently is to simply not destroy the shared mesh collider and take the hit of leaving that guy around. Which isn't actually that offensive for my game.


I've managed to reproduce this, and I know what it is exactly. Just not sure how to "fix" this without making it take a lot longer to initialize. I can give you a tmp fix if you want. Email support at unikronsoftware dot com and I'll give you a script to work around this.
Title: Re: Strange bug, destroying gameobject destroys ALL colliders
Post by: vsn3e8 on May 23, 2013, 10:49:37 pm
So, apparently my workaround does not work on IOS device. Hopefully yours will.



Sure will do.

The workaround I have currently is to simply not destroy the shared mesh collider and take the hit of leaving that guy around. Which isn't actually that offensive for my game.


I've managed to reproduce this, and I know what it is exactly. Just not sure how to "fix" this without making it take a lot longer to initialize. I can give you a tmp fix if you want. Email support at unikronsoftware dot com and I'll give you a script to work around this.
Title: Re: Strange bug, destroying gameobject destroys ALL colliders
Post by: masterhyjinx on June 03, 2013, 10:20:56 am
I just ran into this exact problem tonight.  Unity version 4.1.2f1 and 2D Toolkit 1.92 Final + patch.  I'm going to e-mail you as well because I wouldn't mind the work around either.  Currently, if I destroy an enemy, every other enemy in the scene's mesh collision disappears. 
Title: Re: Strange bug, destroying gameobject destroys ALL colliders
Post by: unikronsoftware on June 03, 2013, 10:32:54 am
Replied to your email.
Title: Re: Strange bug, destroying gameobject destroys ALL colliders
Post by: A3DStudio on June 03, 2013, 12:59:48 pm
Same issue here. What can we do?
Title: Re: Strange bug, destroying gameobject destroys ALL colliders
Post by: unikronsoftware on June 03, 2013, 01:24:39 pm
The issue happens occurs when you instantiate a GameObject that is already in the scene. It reuses the same meshcollider for performance. If you change your Instantiate code to something like this, it should work reliably - but please remember that its quite expensive to create meshcolliders on the fly, you want to cache/pool as many of these as possible rather than deleting them and recreating them later.

Code: [Select]
if (sourcePrefab != null) {
                MeshCollider mc = sourcePrefab.GetComponent<MeshCollider>();

                if (mc != null) {
                    mc.sharedMesh = null;
                }
                tk2dBaseSprite sprite = sourcePrefab.GetComponent<tk2dBaseSprite>();
                if (sprite != null) {
                    if (sprite.meshColliderMesh != null) {
                        Destroy(sprite.meshColliderMesh);
                        sprite.meshColliderMesh = null;
                    }
                }
           
                GameObject g = Instantiate (sourcePrefab) as GameObject;
            }
}