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?
Just to be clear here about the object tree you have here (because I have a suspicion, but we have to confirm it) what you have in your scene is something like:
Column A
Alien 1
Alien 2
Alien 3
Alien 4
Alien 5
Column B
Alien 1
Alien 2
Alien 3
Alien 4
Alien 5
Column C
Alien 1
Alien 2
...
Column D ...
Where each Column is a transform with the first code assigned as a behavior, correct?
@brazmogu It means that void OnBecameInvisible is called when the bullet leaves the camera and destroys it via Destroy(gameObject)
I don't see the dependency you're talking about. when every column has it's ColumnScript, how should they be able to prevent one another to do something? I can't see that happening
Answer by brazmogu · Mar 04, 2017 at 03:22 PM
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);
}
Your answer

Follow this Question
Related Questions
how can i get the gameobject socket by XRsocket interactor 0 Answers
Instatiate a gameobject/prefab on the parent's current location if the child is destroyed. 1 Answer
Cant get setParent to actually set the parent for child. 0 Answers
How to set parent to components object? 1 Answer
Sprite changes the child local position,Sprite pivot change Vector3.zero postion of child gameObject 1 Answer