- Home /
Question by
furybam · Jun 07, 2021 at 02:41 PM ·
errorcollider2dfatal error
Polygon Collider 2D Fatal Error
I have problem with polygon collider 2D
public class provinceManager : MonoBehaviour
{
public Transform[] provinces;
public bool isFirstLaunch;
public Texture2D map;
private Color currentColor;
private (int, int) startX = (0, 0);
private (int, int) endX = (0, 0);
private (int, int) startY = (0, 0);
private (int, int) endY = (0, 0);
void Start()
{
isFirstLaunch = true;
}
void Update()
{
if (isFirstLaunch == true)
{
for (int i=0;i<provinces.Length;i++)
{
map = provinces[i].gameObject.GetComponent<provinceController>().worldMap;
currentColor = provinces[i].gameObject.GetComponent<provinceController>().color;
Color[] c = map.GetPixels(0, 0, map.width, map.height);
Texture2D v2worldmap = new Texture2D(map.width, map.height);
for (int index = 0; index < c.Length; index++)
{
if (c[index] != currentColor)
{
c[index] = Color.clear;
}
}
v2worldmap.SetPixels(c);
for (int y = 0; y < map.height; y++)
{
for (int x = 0; x < map.width; x++)
{
if (v2worldmap.GetPixel(x, y) != Color.clear)
{
if (v2worldmap.GetPixel(startX.Item1, startX.Item2) == Color.clear)
{
startX = (x, y);
endX = (x, y);
}
if (v2worldmap.GetPixel(startY.Item1, startY.Item2) == Color.clear)
{
startY = (x, y);
endY = (x, y);
}
if (startX.Item1 >= x)
{
startX = (x, y);
}
if (endX.Item1 <= x)
{
endX = (x, y);
}
if (startY.Item1 >= y)
{
startY = (x, y);
}
if (endY.Item1 <= y)
{
endY = (x, y);
}
}
}
}
v2worldmap.Apply();
v2worldmap.filterMode = FilterMode.Point;
Rect rec = new Rect(startX.Item1-1f, startY.Item2+2f, endX.Item1-startX.Item1+3f, endY.Item2-startY.Item2-3f);
float posX = (startX.Item1 + endX.Item1) / 2f - (map.width / 2f);
float posY = (startY.Item2 + endY.Item2) / 2f - (map.height / 2f);
Vector2 newPos = new Vector2(posX, posY);
provinces[i].transform.Translate(newPos,Space.World);
provinces[i].gameObject.GetComponent<SpriteRenderer>().sprite = Sprite.Create(v2worldmap, rec, new Vector2(0.5f, 0.5f), 1f);
PolygonCollider2D collider = provinces[i].gameObject.AddComponent<PolygonCollider2D>() as PolygonCollider2D;
}
isFirstLaunch = false;
}
}
}
When I start play mode it shows an error
I want to add component polygon collider 2d to the object with this sprite
No information about this problem in the internet
How to fix it?
province.png
(13.0 kB)
fatalerror.png
(7.3 kB)
Comment
Your answer
Follow this Question
Related Questions
Unity 5 fatal error 3 Answers
"Fatal error! CheckDisalowAllowcation" when making a huge 2d array 2 Answers
Fatal Error, Unity crashes when opening project. 0 Answers
Physics Manager Error 0 Answers