Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Hellwalker 1 · Feb 09, 2011 at 04:44 PM · spawndelayfor-loop

Delaying in for loop, Spawning enemies

Hello, I have code that spawns enemies in for loop. Problem is it spawns them all in one place, one solution is to offset spawning position.I did that at some extent, but script that would delay spawning in for random number of milliseconds would do a better job.

I couldn't make yield work, it did increase number of second for loop ran, but enemies still spawned for every

for ( counter=0; counter<currentWaveCount; counter++){ spawnOffset = transform.position;//reset variable to spawnpoint location spawnOffset.x += Random.Range(spawnOffsetMin, spawnOffsetMax);//randomize position of spawning

 Instantiate(enemy, spawnOffset, transform.rotation);//spawn enemy

}

this is for loop I want to introduce time delay between spawns in.

this is whole code.

var waves : String[]; //Array of enemy waves, must be assigned through inspector view. each element of wave array must contain 3 numbers denoted by "," (comma). exm: 7,4,2. first number is time in seconds wave will spawn at, second is number of enemies to spawn in wave, third is randomizator to second number. var wavesDone : int = 0; //counter for how many waves were already spawned. var currentWaveTime : int;//extracting current wave spawn time from waves string array for easy of use. var currentWaveCount : int;//extracting current wave enemy count from waves string array for easy of use. var enemy: GameObject;//what enemy to spawn. var noMoreWaves: boolean; //boolean to disable spawnpoint after all waves were spawned var currentwave:Array ;//ID of current wave var counter: int; var spawnOffset: Vector3;//variable used to offset enemy position on X axis when Instantiate-ing it., var spawnOffsetMin: float=1.0; var spawnOffsetMax: float=2.0;

//this code adds icon to spawn point game object, to easily select in scene view function OnDrawGizmos() { Gizmos.DrawIcon(transform.position, "TG_EnemySpawner.tif"); }

//we prepare for next wave, extracting and reseting variables function NextWave() { if (wavesDone<waves.length){ //if waves already done are less then lenght of wave array currentwave = waves[wavesDone].Split(","[0]);//extracting variables from waves string for current wave currentWaveTime = int.Parse(currentwave[0]);//assigning current wave spawn time currentWaveCount = int.Parse(currentwave[1])+Mathf.Round(Random.Range(-int.Parse(currentwave[2]),int.Parse(currentwave[2])));//we assign number of enemies to spawn + random number between(-randomizator,+randomizator) (third number from waves string array) } else{ noMoreWaves=true;//if all waves were spawned disable spawner } } //funtion that spawns current wave of enemies function SpawnWave() {

 for ( counter=0; counter&lt;currentWaveCount; counter++){
     spawnOffset = transform.position;//reset variable to spawnpoint location
     spawnOffset.x += Random.Range(spawnOffsetMin, spawnOffsetMax);//randomize position of spawning

     Instantiate(enemy, spawnOffset, transform.rotation);//spawn enemy

 }

wavesDone++; // Get next wave information NextWave(); }

function Start() { NextWave();//get info for first wave }

function Update() { if (noMoreWaves == false){//not all waves were spawned if (Level_Attributes.gameTime >= currentWaveTime){//Timer is run from the start of the game, if time has come to spawn current wave SpawnWave();//spawn wave } } print(Level_Attributes.gameTime);//print timer for debug purphoses. }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Tuck · Feb 09, 2011 at 06:12 PM

There are a couple ways to do this, but I would actually shy away from using a for loop in this situation. One way would be to use Invoke to call a function that Instantiates an enemy, and that function Invokes itself until all enemies have been created. For example:

int iMaxEnemies = 5; int iTotalEnemies = 0; float fSpawnDelay = 5.0f;

void Start() { Invoke("spawnEnemies", fSpawnDelay); }

void spawnEnemies() { //Do your spawn offset stuff here Instantiate(enemy, transform.position /plus spawn offset/, Quaternion.identity); iTotalEnemies++;

if(iTotalEnemies < iMaxEnemies) { Invoke("spawnEnemies", fSpawnDelay); } }

Another way, which I would actually use instead, would be to create a timer to check if it is time to spawn a new enemy. The logic is very similar.

float fNextSpawn = 0.0f; float fSpawnInterval = 5.0f

int iMaxEnemies = 5; int iTotalEnemies = 0;

void Start() { fNextSpawn = Time.time + fSpawnInterval; }

void Update() { if(Time.time > fNextSpawn && iTotalEnemies < iMaxEnemies) { fNextSpawn = Time.time + fSpawnInterval; spawnEnemy(); } }

void spawnEnemy() { Instantiate(enemy, transform.position + offset, Quaternion.identity); iTotalEnemies++; }

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Hellwalker 1 · Feb 10, 2011 at 07:35 AM 0
Share

thx man, I guess I'll have to change logic a bit.

avatar image Joshua · May 24, 2011 at 10:24 PM 0
Share

Awesome answer!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

trying to delay a loop using yield - not working 1 Answer

How do I put a delay in this script ? 1 Answer

delay first spawn 2 Answers

Classic Fireball Shooting Up Obstacle 1 Answer

Adding Delay to a for loop in C# 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges