I know this sounds kinda silly. I just started learning how to use unity and c#. I’m trying to make Conway’s Game of Life to start to learn some stuff. I kinda completed it already but it only works in a 10x10 grid.I want to learn to change the amount of columns and rows so when I run the code everything fits inside the camera. I also don’t know how to change the color of one of the “Squares” in the grid since my code just creates a new object on top of the old one after each generation.
Note: I based my code on this video:
This is my code so far:
void Start()
{
Vertical=(int)Camera.main.orthographicSize;
Horizontal=Vertical;
Grid = new float [c, r];
for(int i=0; i <c; i++)
{
for(int j=0; j<r;j++)
{
if(Random.Range(0.0f,100.0f)>70.0f)
{
Grid[i,j]=1.0f;
}
else
{
Grid[i,j]=0.0f;
}
//Grid[i,j]=Random.Range(0.0f,1.0f);
SpawnTile(i, j, Grid[i,j]);
}
}
InvokeRepeating("Movement", 1.0f, time);
}
private void SpawnTile(int x, int y, float value)
{
GameObject g= new GameObject("x:"+x+"y:"+y);
g.transform.position= new Vector3(x-(Horizontal), y-(Vertical-.5f));
var s =g.AddComponent<SpriteRenderer>();
s.sprite= sprite;
s.color = new Color(value, value, value);
}
private void Movement()
{
Backup=Grid;
int vec;
for(int i=0; i <c; i++)
{
for(int j=0; j<r;j++)
{
vec= Vecinos(i,j);
if((Backup[i,j]==1.0f) && ( vec>3))
{
Grid[i,j]=0.0f;
}else if((Backup[i,j]==1) && (vec>=2 && vec<=3))
{
Grid[i,j]=1.0f;
}else if(Backup[i,j]==0 && vec==3)
{
Grid[i,j]=1.0f;
}else if(Backup[i,j]==1 && vec<2)
{
Grid[i,j]=0.0f;
}
SpawnTile(i, j, Grid[i,j]);
}
}
}