x


Having trouble figuring out coroutines in C#

I'm trying to make an enemy encounter in my game where the enemy will move from left to right and shoots projectiles in 1 second intervals. I've got the movements working fine but I can't get the projectiles to spawn every second. I've checked around and using a coroutine seems to be the way to go about but I can't for the life of me figure out how to use them properly.

The way I have my script set up at the moment, it's spewing out projectiles every frame because I'm calling the coroutine inside Update() but I'm not entirely sure where else I could put it if I want the script to constantly fire until the gameObject is destroyed.

GameObject proj;
IEnumerator timerSpawn()
{
   yield return new WaitForSeconds(1);

   /**Create a temp projectile*/
   GameObject projectile;
   //Instantiate the bullet at the position and rotation of the player
   projectile = Instantiate(proj,transform.position,transform.rotation) as GameObject;   
}

and then in my Update()

void Update () 
{
   StartCoroutine(timerSpawn());
   ...
   ...
}

I'm fairly new to C# and Unity in general so if anyone could help me out on how to go about implementing this, I'd greatly appreciate it.

more ▼

asked Apr 06 '12 at 09:34 PM

HungoverCabbage gravatar image

HungoverCabbage
18 1 1 2

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

If I understand things right, you probably just want to start a looping coroutine from Start() or Awake(). Something like this:

bool keepSpawning = true;

void Start()
{
    StartCoroutine(TimerSpawn());
}

IEnumerator TimerSpawn()
{
    while (keepSpawning)
    {
        Debug.Log("Spawn something");
        yield return new WaitForSeconds(1f);
    }
}

A coroutine will only fire once per call to StartCoroutine(), but as you can see from the example it's possible to keep the function going with a loop. The function will pause on every yield, and will continue execution after the yield.

You can probably figure out where to take it from there.

Don't forget that you can always check the official script reference:

If this is too confusing, remember that you can also use Invoke() or InvokeRepeating().

more ▼

answered Apr 06 '12 at 09:51 PM

rutter gravatar image

rutter
5.2k 2 11

Cheers this worked perfectly.

Apr 06 '12 at 10:06 PM HungoverCabbage
(comments are locked)
10|3000 characters needed characters left

You should use a boolean flag to stop firing coroutines while the last one has not finished:

bool firing = false; // flag "I'm shooting alredy!"

void Update()
{ // only start a new coroutine when the previous one has finished:
  if (!firing) StartCoroutine(timerSpawn());
  ...
}

GameObject proj;
IEnumerator timerSpawn()
{
   firing = true; // the coroutine is running!
   yield return new WaitForSeconds(1);
   /**Create a temp projectile*/
   GameObject projectile;
   //Instantiate the bullet at the position and rotation of the player
   projectile = Instantiate(proj,transform.position,transform.rotation) as GameObject;
   firing = false; // coroutine ended
}

Additionally, you must give velocity to the projectile, or it will not move. Another issue: if created at player position, it will collide with the player. You should usually have an empty game object (childed to the player) at the desired spawn point, and pass this position to Instantiate.

more ▼

answered Apr 06 '12 at 09:43 PM

aldonaletto gravatar image

aldonaletto
41.5k 16 42 197

Thanks for the answer but I actually tried this myself at first and I think because its being called in Update() it still gives the same problem and is called every frame regardless.

Apr 06 '12 at 10:13 PM HungoverCabbage
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x4173
x324
x170

asked: Apr 06 '12 at 09:34 PM

Seen: 2700 times

Last Updated: Apr 06 '12 at 10:13 PM