How to avoid Synchronous Script Behaviour

Hey guys,

I am having problem in building a clone for Space Invaders 1978. My game has 11 columns with 5 rows of aliens. The column has a script that checks for the 1st alien in the row and allows it to fire. The problem is that each column is behaving synchronously with other column scripts Example: if column1 fires a bullet, Column2-11 can’t fire until Column1’s bullet is finished, I want a asynchronous behave such that no columns are dependent on one another.

The ColumnScript is as:

`

public float min = 0;
public float max = 10;

void Start () {
	float rand = Random.Range (min, max);
	Invoke ("SelectForFire", rand);
}

void SelectForFire () {
	if (transform.childCount > 0) {
		transform.GetChild (0).gameObject.GetComponent<EnemyScript> ().Invoke ("Fire", 0f);

		float rand = Random.Range (min, max/2);
		Invoke ("Start", rand);
	}
}

`

and the EnemyScript as:

`

public Rigidbody2D bullet;

void Fire () {
	float x = transform.position.x;
	float y = transform.position.y - 0.4f;
	Instantiate (bullet, new Vector2 (x, y), Quaternion.identity);
}

`

How can I fix this?

So, assuming everything else is in order, the only thing I’m not very familiar with here is the use of Invoke. The docs state it’s as simple as a delayed “SendMessage”, but the impression I’m getting is that it’s less asynchronous that it seems.

Point in case, you could do this with co-routines instead. So the code for your ColumnScript becomes

 public float min = 0;
 public float max = 10;
 void Start () {
     float rand = Random.Range (min, max);
     StartCoroutine (SelectForFire (rand) );
 }
 IEnumerator SelectForFire (float delay) {
     yield return new WaitForSeconds(delay);
     if (transform.childCount > 0) {
         transform.GetChild (0).gameObject.GetComponent<EnemyScript> ().SendMessage ("Fire");
         float rand = Random.Range (min, max/2);
         yield return SelectForFire (rand);
     }
 }

and EnemyScript would be like:

 public Rigidbody2D bullet;
 void Fire () {
     float x = transform.position.x;
     float y = transform.position.y - 0.4f;
     Instantiate (bullet, new Vector2 (x, y), Quaternion.identity);
 }