Question by
pigtripper · Jun 21, 2021 at 09:12 AM ·
spritesruntime
Rows & Columns of Sprites at runtime
I'm new to Unity. I'm trying to instantiate a 3x3 grid of cards with the following code. When I run this, I get cards all the way across and down the screen when I only expect a 3x3 grid. If I remove the loops and call Instantiate, I just get the single card like I expect. What am I doing wrong?
using UnityEngine;
public class BoardBuilder : MonoBehaviour
{
public GameObject Card;
public int width = 3;
public int height = 3;
void Start()
{
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
Instantiate(Card, new Vector3(x*25 - 100, y*-32 + 37, 0), Quaternion.identity);
}
}
}
}
Comment
Answer by pigtripper · Jun 23, 2021 at 11:25 PM
I figured this out. Moving the width and height inside the Start() method (and getting rid of the public scope) fixed it. I'm guessing that having the width and height as a public variables on the component, it's pulling whatever the default width/height values are from the component and not the values I assigned to the variables.