How to instantiate multiple objects at the same time but at different Vector3 positions?

Hello Community,

I would like to instantiate multiple objects at the same time in a vertical arrangement. Currently my code looks like this:

        GameObject hazard = hazards [Random.Range (0, hazards.Length)];
		Vector3 spawnPosition1 = new Vector3 (20, 5, 0);
		Vector3 spawnPosition2 = new Vector3 (20, 3, 0);
		Vector3 spawnPosition3 = new Vector3 (20, 1, 0);
		Vector3 spawnPosition4 = new Vector3 (20, -1, 0);
		Vector3 spawnPosition5 = new Vector3 (20, -3, 0);
		Vector3 spawnPosition6 = new Vector3 (20, -5, 0);
		Quaternion spawnRotation = Quaternion.identity;
		Instantiate (hazard, spawnPosition1, spawnRotation);
		Instantiate (hazard, spawnPosition2, spawnRotation);
		Instantiate (hazard, spawnPosition3, spawnRotation);
		Instantiate (hazard, spawnPosition4, spawnRotation);
		Instantiate (hazard, spawnPosition6, spawnRotation);

Is there a way to instantiate all my hazards at their respective positions in a single line of ‘Instantiate’-code, instead of having to repeatedly instantiate each individual object at each individual position?

Thank you!!

Are you using the AssetsManager pulgin? If you are, then follow up to the 4 chapter. If you try using IIS the connection with fail. Set up your local server to an apache service, like the one included in Xamp , your connection will then be successful. Make sure you are really exporting the bundles

1 Answer

1

Loops and arrays are your friends.

public GameObject[] hazards;

void Start()
{
    GameObject hazard = hazards[Random.Range(0, hazards.Length)];
    Vector3[] spawnPositions = new[] { new Vector3(20, 5, 0), new Vector3(20, 3, 0), new Vector3(20, 1, 0), new Vector3(20, -1, 0), new Vector3(20, -3, 0), new Vector3(20, -5, 0) };
    Quaternion spawnRotation = Quaternion.identity;
    for(int i = 1; i < 6; i++)
    {
        Instantiate(hazard, spawnPositions*, spawnRotation);*

}
}

Thank you so much!!

I'm using the AssetManager plugin, and for some reason my asset bundle server is not starting up. When I tried to run it as administrator I got the same error "[AssetBundleManager] Loading Asset Bundle Manifest: Windows Windows is not a valid asset bundle." If I'm not even able to run this locally my real concern is http://answers.unity3d.com/questions/1297628/make-a-single-app-for-multiple-games-using-unity-a.html