Change Polygon Collider With Script?

Hello,

I have a “main” sprite with several other sprites as children (not germane, but just FYI).
The main sprite starts out with a certain image and polygon collider attached.

During the course of the game I need to change the “Sprite” property and the Polygon collider of the “main” sprite.

I have another sprite #2 set up with its own matching polygon collider.
I really just want to swap out the sprite image and polygon collider of the “main” sprite with those from sprite #2.

Help Mr. Wizard!!!

Hope that explanation is not too muddled.
Thanks for reading.

– Paul

Though I’ve never done it, I don’t know why you couldn’t just add multiple PolygonCollider2D components and/or multiple SpriteRenderer components to the same game object. Then, simply enable/disable them as appropriate via a script.

Jeff

J,

Thanks for reply.
I hadn’t even thought about that, but I tried it.
You can add multiple colliders but only one SpriteRenderer.

– Paul

Yeah, you’re right (I just tried it here). In that case, I guess you could…

  • Create 2 GameObjects, one for each state and just switch them between them as needed
  • Create a single GameObject with 2 colliders and a single sprite renderer. Then, enable/disable the colliders as necessary and switch the assigned sprite as necessary.

Jeff