Hello Guest

Author Topic: Colliders messed up after SetActive(true)  (Read 9259 times)

bendahmon

  • Newbie
  • *
  • Posts: 10
    • View Profile
Colliders messed up after SetActive(true)
« on: August 17, 2013, 11:06:15 am »
I have a level which is all under a GameObject. When I return to the level selector of my game, I call SetActive(false) on the "root" GameObject for the actual game and show my level selector instead. However, when I return to the game by calling SetActive(true) on my game GameObject, the colliders of some of my sprites are messed up. Actually, it seems like my objects are using another object's collider...

Is this a bug, or is there something I'm doing wrong? :)

Version used: 2.1 final

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Colliders messed up after SetActive(true)
« Reply #1 on: August 17, 2013, 12:32:11 pm »
Is it a polygon collider? Are you instantiating it or is it just in the scene?

bendahmon

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Colliders messed up after SetActive(true)
« Reply #2 on: August 17, 2013, 01:50:36 pm »
Yes, polygon collider.

The object is "just in the scene". The only think I do that I can think is relevant is that I'm deactivating and activating one of its ancestors.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Colliders messed up after SetActive(true)
« Reply #3 on: August 17, 2013, 06:44:45 pm »
Is it correct just before its disabled? As far as I remember, the code will not update any mesh colliders after they have been created, and nothing happens OnEnable - and Awake should just get called once on these sprites. I'm at a loss as to what to suggest - except to set a breakpoint / spam some debug.logs to try and find out whats going on with them.

The mesh collider mesh isn't shared and shouldn't get updated, so if something happens to it it must just be in that script, and there are very few places that could happen.

bendahmon

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Colliders messed up after SetActive(true)
« Reply #4 on: August 17, 2013, 06:59:02 pm »
Ok, I think I can explain this better.

I have a sprite with a polygon collider. When I select it in unity I also see the collider gizmo. Then I pause the game. Then I deactivate the sprite itself and re-activate it. Now the sprite has a new collider from one of the other sprites.

bendahmon

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Colliders messed up after SetActive(true)
« Reply #5 on: August 17, 2013, 07:05:14 pm »
By "pause the game" I mean using the pause button in the Unity editor.

It also seems to only affect the sprites that are deactivated/activated, either directly or by a parent.

bendahmon

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Colliders messed up after SetActive(true)
« Reply #6 on: August 17, 2013, 07:07:00 pm »
It also happens if I only activate/deactivate the mesh collider of the sprite. (Narrowing things down? :) )

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Colliders messed up after SetActive(true)
« Reply #7 on: August 17, 2013, 07:25:15 pm »
No actually - thats making it sound more and more like its a bug in Unity. Is it changing if you simply just disable and enable the meshcollider component?

bendahmon

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Colliders messed up after SetActive(true)
« Reply #8 on: August 17, 2013, 07:33:53 pm »
Yes, if I disable/enable the mesh collider, it will have a different collider when activated.

However, if the game object is not enabled from the beginning, it seems like I can activate/deactivate it as much as I want.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Colliders messed up after SetActive(true)
« Reply #9 on: August 17, 2013, 08:05:40 pm »
We might be getting somewhere with this.
Are any of these objects prefabs? Anywhere from the object all the way up to the root

bendahmon

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Colliders messed up after SetActive(true)
« Reply #10 on: August 17, 2013, 08:11:17 pm »
No, there are prefabs alongside these objects (siblings?), but the object in question is not a prefab, and neither are any of its ancestors.

However, some of my other sprites are prefabs and the same thing would happen to them.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Colliders messed up after SetActive(true)
« Reply #11 on: August 17, 2013, 08:22:08 pm »
Ok. So its not that issue then.
This is looking very much like an issue with Unity.
What happens if you double click on the meshCollider mesh in the inspector? Does it look right when it is disabled?
Next thing to do is to try is to disable the tk2dSprite AFTER disabling the meshcollider, and then turn the meshcollider back on by itself. Check the mesh, and then turn on the sprite component and check the mesh again.

bendahmon

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Colliders messed up after SetActive(true)
« Reply #12 on: August 18, 2013, 02:17:17 pm »
Actually, the mesh collider doesn't look correct from the beginning. The gizmo looks correct, but when I double click on the mesh collider it's wrong (while the gizmo still looks right)... I think that should answer your last questions.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Colliders messed up after SetActive(true)
« Reply #13 on: August 18, 2013, 02:31:36 pm »
I've more or less run out of things to suggest. Would it be possible to set up a stripped down repro case for me? support at unikronsoftware.com if possible.

bendahmon

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Colliders messed up after SetActive(true)
« Reply #14 on: August 18, 2013, 06:20:32 pm »
I think it just hit me....

What I did with these sprites is that I copied them (the game object) and just switched to another sprite. I think the collider wasn't properly switched with them (I say properly, because it works to some degree)

Does that help you at all?

If not I will try to clone my project and try to trim it down to the minimum and send you.