- Home /
Problem with adding dynamic polygon collider 2d
When attaching a PolygonCollider2D to a Sprite with this line: gameObject.AddComponent();
It adds a polygon collider of the same shape as the sprite. For some reason this only works in the editor. On web or windows builds the colldier is messed up and usually much bigger or smaller. Am I doing something wrong or is it just a bug?
$$anonymous$$ight be a bug. How come you are creating it through scripting though? Why not have it pre-added, and merely disable it when you don't want it to be used?
Because the sprite is taken randomly from an array. So I can't really know what the collider should look like.
But this sprite array is still a limited amount of sprites. Why not just have them be existing prefab objects with polygon colliders already attached? Generating a new polygon collider during gameplay is an expensive operation.
$$anonymous$$ight be, but it happens only once in the beginning of a level, only for the main character. All other colliders are primitives. For a one-time operation, it's not that expensive.
True about the array, but that way you can't download new graphics from the web for example or create them at run time.
I actually currently create premade colliders for everything in the array and it works, so I guess I don't need help anymore on how to solve it, but more to find if it's a bug I guess :D
Thanks!
I also have this issue. Works fine in the editor, but not in the web player. I am wondering if it occurs because of any optimization being done.
Answer by Kalocyte · Aug 07, 2014 at 06:54 PM
I found the solution to this problem if someone still runs into it. I checked my output log from the development build and it said:
Sprite outline generation failed - could not read texture pixel data. Did you forget to make the texture readable?
You have to set your sprites' type to Advanced and enable the Read/Write box. At least the ones on which you are going to put a Polygon Collider.
HI $$anonymous$$alocyte is it possible to set sprites' type to Advanced and enable the Read/Write box at run time
I dont understand why this isn't the best answer, because its THE answer. Anyway, hopefully the poster @Amitloaf will fix that :P I rewarded you points, @$$anonymous$$alocyte for your awesome help!
$$anonymous$$an... I've been spending 2 days to solve this problem!!! THAN$$anonymous$$ YOU!! I've just sign-in first time to the forum just to thank you! :)
In case anyone else runs into this, I also needed to set the Sprite Atlas that the sprite belongs to to "Enable Read/Write".
Your answer
Follow this Question
Related Questions
Problems with sprites. 0 Answers
Flickering Sprites 0 Answers
Collider Mask Bug 0 Answers
Finding a certain variable is failing 0 Answers
How to achieve 2D pixel perfect collision physics (like Badland) in Unity3D ? 3 Answers