At start of my 2D unity game I want to create a new "building" (at random).
So I created a prefab named "Building" from a polygon sprite. Then I created a "Empty Game Object" named "GameManager" with a script named "GameManager" attached to it. The script looks like this:
public class GameManager : MonoBehaviour
{
public GameObject Building;
// Start is called before the first frame update
void Start()
{
var createdBuilding = Instantiate(Building, new Vector3(0, 0, 0), Quaternion.identity);
var sprite = createdBuilding.GetComponent<SpriteRenderer>().sprite;
Vector2[] geometry = new Vector2[6];
geometry[0] = new Vector2(0,0);
geometry[1] = new Vector2(0,20);
geometry[2] = new Vector2(20,20);
geometry[3] = new Vector2(27,20);
geometry[4] = new Vector2(29,29);
geometry[5] = new Vector2(16,0);
sprite.OverrideGeometry(geometry, sprite.triangles);
}
}
The hardcoded position values are just for testing purporses. The plan is to create multible buildings but first I have to get unity to show at least one of them.
Afterwards I attached the prefab "Building" to the variable "Building" via the inspector.
But when I ran the game the sprite is shown as a Pentagon, regardless what I write into the "geometry"-Array.
So why I cant change the geometry? And is this even the right way to create buildings at startup?