/// <summary> /// Generate star if randomNumber is even /// </summary> /// <param name="posX"></param> private void GenerateStar(float posX) { int randomNumber = Random.Range(0, 30); if (randomNumber % 2 == 0) { StarController star = GetOrCreateStar(); star.transform.position = new Vector3(posX, 0); star.gameObject.SetActive(true); } }
/// <summary> /// Get or create star /// </summary> /// <returns></returns> private StarController GetOrCreateStar() { StarController star = starsPool.Find(s => !s.gameObject.activeSelf); if (star == null) { star = Instantiate(starPrefab.gameObject, transform).GetComponent <StarController>(); starsPool.Add(star); } return(star); }