protected virtual void InitializeInStart() { // if is not singleton destroy it if (singleton == null) { singleton = this; prefab = gameObject; } else { if (this != singleton) { Destroy(gameObject); } } if (speed <= 0) { speed = DEFAULT_SPEED; } //spawn chickens chickenSize = chickenPrefab.transform.localScale * chickenPrefab.GetComponent <BoxCollider2D>().size; xGap = chickenSize.x + xGap; yGap = chickenSize.y + yGap; SetArmyPosition(); SetMoveXLimits(); StartCoroutine(SpawnChickens()); }
private void OnDestroy() { if (singleton != null) { if (this == singleton) { singleton = null; wholeChickenNumber = 0; } } }
/// <summary> /// For now just simple destroy and instantiate. /// </summary> void CreateNewChickenArmy() { prefab = Instantiate <GameObject>(prefab, Vector3.zero, Quaternion.identity); singleton = prefab.GetComponent <ChickenArmyController>(); prefab.name = name; //because xGap and yGap are modified depending on thier previous values we need to change them as they will be //again with their first values (we subtract the addition in modification) singleton.xGap -= chickenSize.x; singleton.yGap -= chickenSize.y; Destroy(gameObject); }
IEnumerator GetToPosition() { ChickenArmyController chickenArmy = ChickenArmyController.Singleton; Vector2 purposePos = chickenArmy.GetPosition(columnIndex, rowIndex); while (Vector2.Distance(purposePos, transform.position) > speed * Time.deltaTime) { transform.position += ((Vector3)purposePos - transform.position).normalized * speed * Time.deltaTime; purposePos = chickenArmy.GetPosition(columnIndex, rowIndex); yield return(null); } isSetInPosition = true; chickenArmy.AddChicken(this, columnIndex, rowIndex); }