Trying to get gameobject from one list to follow gameobjects in another list
I have 3 instances of a single gameobject (hollow bubbles) which im moving around in my game screen. That has been successfully achieved but now im trying to create 3 another instances of gameobject (the inner bubbles) which follow the first 3 instances of hollow bubbles.
I have created 2 Lists which hold these 3 instances respectively and an array that holds in Element 0 the sprite that i need to add to the inner bubbles that follow the hollow bubbles.
public Sprite[] innerBubblesSprites;
private List<GameObject> yellowBubbles = new List<GameObject>();
private List<GameObject> innerYellowBubbles = new List<GameObject>();
void GameLogic() {
for (int j = 0; j < 3; j++) {
if (i == 0) {
Vector2 yellowPos = new Vector2 (Random.Range (-2.5f, 2.5f), -9.8f);
yellowBubbles.Add(Instantiate(bubbles[i], yellowPos, Quaternion.identity) as GameObject);
yellowBubbles[i].transform.localScale = new Vector2 (bubblesScale, bubblesScale);
StartCoroutine ("BlinkingBubbles");
}
}
IEnumerator BlinkingBubbles() {
for (int i = 0; i < 3; i++) {
innerYellowBubbles[i] = Instantiate (innerBubblesGameObjects [0], yellowBubbles [i].transform.position, Quaternion.identity) as GameObject;
innerYellowBubbles[i].GetComponent<SpriteRenderer> ().sprite = innerBubblesSprites [0];
yield return null;
}
}
The 3 instances of innerYellowBubbles are spawning fine in the object hierarchy but all seem to be spawning at the same position and the sprite is also not getting assigned. Further only the first yellowBubble is getting its localScale to the values ive set, otherwise they revert to their default scaling of 1.0. And there is an argument out of range exception. Can somebody please point me in the right direction? Any help would be appreciated. Next ive to make the innerYellowBubbles follow the YellowBubbles as well.
Answer by captainfrogduh · Jan 31, 2017 at 12:01 PM
Looks like i was using Coroutines in a way that they are not meant to. Solved this problem. Heres a great explanation of using Coroutines for not so 'technical' (newbie) programmers :)
http://answers.unity3d.com/questions/276055/question-about-coroutines-and-waiting-for-a-functi.html
Your answer
Follow this Question
Related Questions
Trying to program two buttons to appear when the player in my game dies 0 Answers
Certain animation states not playing on mobile. Not sure if it's my code or a bug. 0 Answers
Cycling through array not working. Pulling my hair over this one. 0 Answers
List resulting in out of range 0 Answers
Unity won't print last item in my array for my GameObjects 0 Answers