Generating Objects

So i was trying to create a button that once pressed (UI button) it will generate a given number of objects, but something doesn’t work, as it tells me to fix all compiler errors.

Here is the code:

public class Gen_Food : MonoBehaviour {

public GameObject Food;
public int counter=0;
public float Pos_x;
public float Pos_z;

public void AddObject()
{
while(counter<200)
{
Pos_x = Random.Range(-5.69f,18f);
Pos_z = Random.Range(-14.8f,12f);
Instantiate(Food, new Vector3(Pos_x, -2.85, Pos_z),Quaternion.identify);
counter += 1;
}

}

}
Edit: changed the code a little.

What are your compiler errors? Open up the console by clicking on the status bar along the bottom of the Unity window. You can copy and paste out of that window.

Also, use code tags when posting code.

At a glance, it appears that Pos_x and Pos_z should be float, not int.

Here are the errors i get, also i changed the code a little, see in edit.5211647--518867--Screenshot_743.png

Need an “f” after “-2.85”, and the last parameter should be “identity” not “identify”.

1 Like

Alright, everything works now. Thanks so much for your help!