Move objects/prefabs from left to right and destroy them after some time

Hello Everyone,

My first question here, new to unity, so excited! Quick one though;

I’m making a 2D game. I have a prefab of the free 2D dragon asset. How can I spawn the same object from left to right, multiple times at random y axis within the range of the screen height(orientation landscape). Have it move across the screen to the other side and then destroy it after it crosses the screen.

Please, find attached an image of a visual representation of what I’m trying to achieve. How do I implement this please using C#. I need to instantiate the prefab at runtime and then move it across, all using code or if there’s a better way to do this, I’ll really appreciate the assistance.

Thanks again.

![alt text][2]

Hello, your question is quite complex, and you should understand the code more than copy/paste some code.
Firstly you need to instantiate your prefab:

  GameObject dragon = (GameObject)Instantiate(dragonPrefab, new Vector3(X,Y,Z), Quaternion.identity);

You can do it multiple times (in a loop for example) if you need more than one object.

You can place the instance randomly using a random number instead of Y in the Vec3 above.

Then you need to make your dragon move, you can use the Unity physics but I’m not familiar with it, you should watch some tutorials.

If you plan to move it on a single axis, you don’t have to use Unity physics, you can do it with code easily.

void Update ()
{
    dragon.transform.position.Set(dragon.transform.position.x, dragon.transform.position.y + speed, dragon.transform.position.z);
}

And finally, you need to destroy it:

    if (dragon.transform.position.x > limit)
        Destroy(dragon);

Hope this helps. Try to ask more specific question than the goal you want to achieve.