《学习创建一个随机产生的物体的代码》

using UnityEngine;
using System.Collections;

public class FoodSnake : MonoBehaviour {
public int yLimit = 30;
public int xLimit = 22;
public GameObject SSFood;
// Use this for initialization
void Start ()
{
InvokeRepeating("Food", 1, 4);
}

// Update is called once per frame
void Food()
{
int x = Random.Range(-xLimit,xLimit);
int y = Random.Range(-yLimit,yLimit);

//Quaternion.identity=旋转的角度0.0.0.0 SSFood是这个cube   new Vector2(x, y) 物体出现的位置

Instantiate(SSFood, new Vector2(x, y), Quaternion.identity);
}
void Update ()
{

}
}

原文地址:https://www.cnblogs.com/ylllove/p/6106433.html