Reputation: 4806
Attempting to create a grid of a simple circle gameobject as soon as the game starts. The space between each circle needs to be 1.41 and 1.34 in 2D. So with a little maths, I would have thought that this script would create this grid relative to an initial reference object.
However, upon clicking play in the editor, the game simply freezes and I have to kill Unity with my command prompt.
Any ideas?
Here is the code:
void Awake()
{
Transform transform = gameObject.GetComponent<Transform>();
for (float i = 1; i < 8; i++)
{
for (float j = 1; j<8;j++)
{
Instantiate(gameObject, transform.position + new Vector3(i * 1.41f, 0, j * 1.34f), new Quaternion(0, 0, 0, 0));
}
}
}
Upvotes: 0
Views: 2809
Reputation: 10701
void Awake()
{
Transform transform = gameObject.GetComponent<Transform>();
for (float i = 1; i < 8; i++)
{
for (float j = 1; j<8;j++)
{
Instantiate(gameObject, transform.position + new Vector3(i * 1.41f, 0, j * 1.34f), new Quaternion(0, 0, 0, 0));
}
}
}
Looking into your Instantantiate, you are using gameObject. This is the reference to the game object holding that script. So what you seem to be doing is instantiating a clone of that object. The newly created object is also triggering the loop, this will start a new process of creation and so on.
All in all, you created an endless loop. You need to create an instance of something else, a tile prefab most likely.
public GameObject myTilePrefab;
void Awake()
{
Transform transform = gameObject.GetComponent<Transform>();
for (float i = 1; i < 8; i++)
{
for (float j = 1; j<8;j++)
{
Instantiate(myTilePrefab, transform.position + new Vector3(i * 1.41f, 0, j * 1.34f), new Quaternion(0, 0, 0, 0));
}
}
}
Upvotes: 3