So I have this GO which supposed to shoot multiple bullets everytime it changes its designated spot/position. Unfortunately, it only instantiates one bullet everytime it changes position. I’m using object pooling to instantiate the bullet. I think the issues here are my loops and coroutine but can’t still figure it out.
Here’s my messy code.
PLEASE TAKE NOTE: I didn’t include all the variables here but I declared everything in my actual code, so don’t worry about it.
public GameObject bullet; void Start ()
{
// OBJECT POOLING RIGHT HERE!!!
// To store pooledAmount of game objects once the game starts
pooledAmount = 50;
bulletS = new List<GameObject> ();
for (int i = 0; i < pooledAmount; i++) {
GameObject obj = (GameObject)Instantiate (bullet);
obj.SetActive (false);
bulletS.Add (obj);
}
StartCoroutine ("Pattern");
}
IEnumerator Pattern ()
{
// First Pattern Spot
while (transform.position != patternSpot [0].position) {
transform.position = Vector2.MoveTowards (transform.position, posXY, movementSpeed * Time.deltaTime);
yield return null;
}
StartCoroutine ("Shoot", 1f);
yield return new WaitForSeconds (3f);
// Second Pattern Spot
while (transform.position != patternSpot [1].position) {
transform.position = Vector2.MoveTowards (transform.position, posXY1, movementSpeed * Time.deltaTime);
yield return null;
}
StartCoroutine ("Shoot", 1f);
yield return null;
}
IEnumerator Shoot () {
//Don't pool any game objects that is already active
for (int i = 0; i < bulletS.Count; i++) {
if (bulletS *!= null) {*
_ if (!bulletS .activeInHierarchy) {_
_ bulletS .transform.position = transform.position;
bulletS .transform.rotation = transform.rotation;
bulletS .SetActive (true);
* break;
}
} yield return new WaitForSeconds (.1f);
}
}*_