Best Way to create a level of moving obstacles in 2D game

Hello, i am new on unity, got like 2 days of experience and i am learning everything that i can

Actualy i am doing a 2D game and my idea is make a “Hero” dogheing some moving obstacles coming from righ to left side

My question is:
Should i create 100 gameobjects manually (like dragging from assets)?
Or there is a away to instantiate outside camara vision, give them some velocity.x to move them, and destroy them when they go out of the screen?

thanks, i hope my question was clear :confused:

Use this script on a block to instantiate the object every 2 seconds.

public GameObject spawnitem;

void Start () {
InvokeRepeating("Go", 2f,2f);
}

void Go () {
Instantiate(spawnitem,this.transform.position,this.transform.rotation);
}

Then put this on the instantiated object to have it move left or right when spawned.

void Update () {
     transform.Translate(Vector3.left * 0.15F);
	}

There are many, many ways to do this. The worst would be manually dragging the objects from the assets.

A good way to do it is to make an empty game object called “GameManager” and attach a script to it that will manage the instantiation of obstacles.

In the top of the script, declare a public GameObject, and then drag your obstacle prefab into the script in the inspector.

public GameObject obstacle;

As you can imagine, you can have multiple GameObjects and make the script select one to instantiate.

To instantiate a GameObject use:

GameObject.Instantiate(obstacle, position, rotation);

On each prefab I make a script to make it move. You can use the Transform.Translate function:

myTransform.Translate(Vector3.left * speed * Time.deltaTime, Space.World);

Also check if the object is beyond the boundaries of the screen. It is smart to have the far left of the screen be equal to 0 on the x-axis. This way, you can easily check if the x-coordinate of the object is below 0 in the same script as the “moving script”. Remember that Transform.position.x gives you the center of your object, so you have to (in this case) add half the width of the object, to get the x-coordinate of the right side of your object.

To destroy the object (in a script attached directly to it):

Destroy(this.gameObject);

I hope this leads you in the right direction. Try experimenting and reading the Unity Script Reference about things you do not yet understand: http://docs.unity3d.com/Documentation/ScriptReference/

I recommend that you instantiate them at start of game.
Make a Generic List (or your array/list of choice) to keep reference to them, position and then deactivate them.

When you no longer need them, instead of destroying them you reposition, reset all values needing to reset, and deactivate them again.

Instantiate is a “costly” action, and should be done when the player is expecting load times.

This has been discussed many times before. Try a web search for making a pool of objects unity3d

Prime benefit over making them in editor is that you can have one single prefab to make changes to when needing to.