Unity - A计划(一年有效期) 扫二维码继续学习 二维码时效为半小时

(61评价)
价格: 2208.00元
贪吃蛇那个不能实例化食物
猫懒发起了问答2018-09-12
3
回复
341
浏览
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);
    }

这是代码  运行报错不出现食物

所有回复
发表回复
你还没有登录,请先 登录或 注册!