public void CleanUp(Transform bg) { //need to setActive(false) for all objects not used ; if not on screen string n = bg.gameObject.name; Debug.Log(string.Format("bg name: {0}", n)); switch (n) { case "b1": obstacles = bg.Find("1/obstacles1"); Debug.Log(string.Format("obstacles1-CLEAN")); break; case "b2": obstacles = bg.Find("2/obstacles2"); Debug.Log(string.Format("obstacles2-CLEAN")); break; case "b3": obstacles = bg.Find("3/obstacles3"); Debug.Log(string.Format("obstacles3-CLEAN")); break; } foreach (Transform o in obstacles) { o.gameObject.SetActive(false); MultipleBall buff = o.GetComponent <MultipleBall>(); if (buff != null) { Destroy(buff); } o.transform.SetParent(Poolob.transform); //Pool is empty object } }
void createNew(string typeKey, int num) { //function for creating new objs: if list of type<shape> has object, setActive(true), put in background // else create new object, put in background // Destroy has high cost -- when obj not needed, setActive(false), put into list of type<shape>; //Need to make sure all created object names are 'GameObject', due to ball script randomize rando = gameObject.GetComponent <randomize>(); obstacles = rando.getObstacles(); Debug.Log(string.Format("obstacle Name: {0}", obstacles.gameObject.name)); List <GameObject> l = pool[typeKey]; // foreach(GameObject g in l){ // if (g == null){ // l.Remove(g); // } // } if (obstacles.childCount > 0) { foreach (Transform gObj in obstacles) //add existing shapes to list (by type) { if (gObj.gameObject.CompareTag("c")) { pool["c"].Add(gObj.gameObject); } else if (gObj.gameObject.CompareTag("r")) { pool["r"].Add(gObj.gameObject); } else { pool["p"].Add(gObj.gameObject); } } } //if(pool.TryGetValue(typeKey, out temp)){ // print("Invalid typeKey given to createNew"); //shouldn't return null //} //else if (l.Count == 0) //if list is empty, add shape num times ; This case MAY BE REDUNDANT { switch (typeKey) { case "c": for (int i = 0; i < num; ++i) { GameObject c = (GameObject)Instantiate(circlePrefab); //instantiate clones prefab c.name = "GameObject"; c.transform.SetParent(obstacles); addBuff(c, l); } break; case "r": for ( ; num != 0; num--) //no initialization is ok for for statement? { GameObject r = (GameObject)Instantiate(rectanglePrefab); r.name = "GameObject"; r.transform.SetParent(obstacles); addBuff(r, l); } break; case "p": for (int i = 0; i < num; ++i) { GameObject p = (GameObject)Instantiate(pentagonPrefab); p.name = "GameObject"; p.transform.SetParent(obstacles); addBuff(p, l); } break; default: break; } } else //there's something in list { int count = 0; foreach (Transform gObj in obstacles) { if (gObj.gameObject.CompareTag(typeKey)) //find # of shape in THIS background { count++; } } if (count >= num) //list has more/equal to num; excess { for (int i = 0; i < num; i++) { GameObject g = l[i]; MultipleBall buff = g.GetComponent <MultipleBall>(); if (buff == null) //only regular shapes are set to active { g.SetActive(true); } } } else if (count < num) //list has fewer elements than num { for (int i = 0; i < count; i++) { GameObject g = l[i]; MultipleBall buff = g.GetComponent <MultipleBall>(); if (buff == null) //only regular shapes are set to active { g.SetActive(true); } } switch (typeKey) { case "c": for (int i = 0; i < num - count; ++i) { GameObject c = (GameObject)Instantiate(circlePrefab); c.name = "GameObject"; c.transform.SetParent(obstacles); addBuff(c, l); } break; case "r": for (int i = 0; i < num - count; ++i) { GameObject r = (GameObject)Instantiate(rectanglePrefab); r.name = "GameObject"; r.transform.SetParent(obstacles); addBuff(r, l); } break; case "p": for (int i = 0; i < num - count; ++i) { GameObject p = (GameObject)Instantiate(pentagonPrefab); p.name = "GameObject"; p.transform.SetParent(obstacles); addBuff(p, l); } break; } } } }