0
votes

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);
}

}

1

1 Answers

3
votes

These are the Instantiate overload functions:

public static Object Instantiate(Object original);
public static Object Instantiate(Object original, Transform parent);
public static Object Instantiate(Object original, Transform parent, bool instantiateInWorldSpace);
public static Object Instantiate(Object original, Vector3 position, Quaternion rotation);
public static Object Instantiate(Object original, Vector3 position, Quaternion rotation, Transform parent);

When you call Instantiate(garbage, garbagePosition); with two parameters, it will use the public static Object Instantiate(Object original, Transform parent); overload because that's the closest overload since it takes two parameters.

But garbagePosition is a Vector3 while the second parameter expect a Transform not Vector3.

You need to use the public static Object Instantiate(Object original, Vector3 position, Quaternion rotation); overload. So in order to pass in the Vector3 position, you must also pass in the rotation/Quaternion.

If you don't have a rotation to pass it, you can just pass it Quaternion.identity which means no rotation.

Instantiate(garbage, garbagePosition); 

should be

Instantiate(garbage, garbagePosition, Quaternion.identity);