public class FoodMaker : MonoBehaviour {
public int xlimit = 16;
public int ylimit = 11;
public int xoffset = 7;
public GameObject foodPrefab;
public Sprite[] foodSprites;
private Transform foodHolder;
void Start()
{
foodHolder = GameObject.Find("foodHolder").transform;
MakeFood();
}
void MakeFood()
{
int index = Random.Range(0, foodSprites.Length);
GameObject food = Instantiate(foodPrefab);
food.GetComponent<Image>().sprite = foodSprites[index];
food.transform.SetParent(foodHolder, false);
int x = Random.Range(-xlimit + xoffset, xlimit);
int y = Random.Range(-ylimit, ylimit);
food.transform.localPosition = new Vector3(x * 30, y * 30, 0);
}
这是代码 运行报错不出现食物