Reputation: 3
Every time I press the play button in unity it just sits there doing nothing and I think it has something to do with the Instantiate Syntax here is my code and this is the only script I have running. I'm trying to just simply make a grid of my GameObject. if anyone knows why that would be great or if not maybe you know a better way to do it. Thanks!!
using UnityEngine;
public class Script2 : MonoBehaviour
{
public GameObject Sprite;
void Start()
{
Generate();
}
public void Generate()
{
float width = Sprite.transform.lossyScale.x;
float height = Sprite.transform.lossyScale.y;
for (int y = 0; y <= 100; y += 10)
{
for (int x = 0; x <= 100; y += 10)
{
GameObject Square = Sprite;
Instantiate(Square, new Vector2(x * width, y * height), Quaternion.identity);
}
}
}
}
Upvotes: 0
Views: 198
Reputation: 26
Your x loop has an error, so it ran into infinite loop. Should be x+=10
for (int x = 0; x <= 100; x += 10)
{
GameObject Square = Sprite;
Instantiate(Square, new Vector2(x * width, y * height), Quaternion.identity);
}
Upvotes: 1
Reputation:
The best solution I have is the method I use when creating a grid of prefabs which is this method
using UnityEngine;
public class Class1 : MonoBehaviour
{
public GameObject Prefab;
private void Start ()
{
Generate ();
}
public void Generate ()
{
float width = Prefab.transform.lossyScale.x;
float height = Prefab.transform.lossyScale.y;
for ( int y = 0; y < 100; y++ )
{
for ( int x = 0; x < 100; x++ )
{
Vector3 position = new Vector3 ( x * width, y * height );
Instantiate<GameObject> ( Prefab, position, Quaternion.identity );
}
}
}
}
Upvotes: 0