Jump to content

(UNITY, C#) How to make each individual enemy spawn in random positions

Hey guys, I've been working on this project and I'm trying to spawn the enemies in random positions but they all spawn together in a random position, how would I make each individual enemy spawn in a random position. 

	public GameObject Enemy;

	public TextMesh text;

	private int i = 0;

	void Start () {
		text.text = "Round 1";
	}
	// Update is called once per frame
	void Update () {
		Vector2 RandPos = new Vector2 (Random.Range(-15f, 15f), 0f);
		while (i < 5) {
			Instantiate (Enemy, RandPos, transform.rotation);
			i++;
		}

In the code the instantiation spawns an enemy prefab 5 times in the random position between -15 and +15 on the x axis, but all 5 enemies spawn together and i want them to spawn in individual positions.

 

any help will be appreciated :)

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, elpiop said:

move the Vector2 Randpos = ... into the while loop

 

2 minutes ago, Positive7 said:

Move the RandPos inside while (i < 5) {

thanks guys i appreciate it, oh i get it now because each time it loops it will choose a random position before the enemy spawns :) 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×