I'm building a game and have run into a problem. I'm trying to instantiate an object when the "Jump" button is pressed, and I'm trying to give the instantiated object a random position between a specified location. However, my Instantiate function is only accepting a "Transform" variable as it's second argument, and not taking in any Vector3 values for position, as it generally does.
Can anybody please tell me why, and what is the solution to this problem? This is the code: Please note the Instantiate function in the Update function, that's where I'm running into the problem. I'd like to use Vector3 garbagePosition as the second argument. Thanks.
public Transform ground;
public GameObject garbage;
public float loadTime;
public int numberOfObjects;
private Queue<Transform> groundQueue;
public Vector3 startPosition;
public Vector3 nextPosition;
// Use this for initialization
void Start () {
nextPosition = startPosition;
groundQueue = new Queue<Transform>(numberOfObjects);
for(int i = 0; i<numberOfObjects; i++)
{
groundQueue.Enqueue((Transform)Instantiate(ground));
}
for(int j = 0; j<numberOfObjects; j++)
{
Recycle();
}
}
// Update is called once per frame
void Update () {
if (groundQueue.Peek().localPosition.z + 10 < PlayerScript.playerDistance)
{
Recycle();
}
if (Input.GetButtonDown("Jump"))
{
Vector3 garbagePosition = new Vector3(Random.Range(-4, 4),
0.5f,
PlayerScript.playerDistance + Random.Range(10, 20));
Instantiate(garbage, garbagePosition);
loadTime += Time.time;
}
}
private void Recycle()
{
Transform item = groundQueue.Dequeue();
item.localPosition = nextPosition;
nextPosition.z += item.localScale.z;
groundQueue.Enqueue(item);
}
}