Hanaan328
Hanaan328

Reputation: 7

Object instantiates on top of the previous object

I've written a script where a level is supposed to be instantiated when my character hits a specific location. It works perfectly the first time it's meant to instantiate. But on the second time, it instantiates the level directly on top of the previous level. Here's a video of whats happening: https://vimeo.com/283706809 And here's the code (It isn't all of the code, only the code relevant to this question):

if (collision.tag == "Score2")
{
    scoreText.text = (++score).ToString();
    Destroy(collision.gameObject);
    int randomNumber = Random.Range(0, 2);
    if (randomNumber == 0)
        Instantiate(course[0], new Vector2(-0.39f, -2f + transform.position.z), transform.rotation);
    else
        Instantiate(course[1], new Vector2(-0.39f, -2f + transform.position.z), transform.rotation);
    return;

Upvotes: 0

Views: 45

Answers (1)

Scornz
Scornz

Reputation: 412

Since it is a 2D game, you shouldn't really be using the 'z' of the Vector3 class. I think you might've meant to put new Vector2 (-0.39f, -2f + transform.position.y) instead of new Vector2(-0.39f, -2f + transform.position.z).

Upvotes: 1

Related Questions