Exemplo n.º 1
0
        private void GenerateCoin()
        {
            if (coinPrefab == null)
            {
                Debug.LogError("Can't generate coin from null prefab");
                return;
            }

            // Don't respawn a collectible if player is nearby.
            if (isFirstCoin)
            {
                isFirstCoin = false;
            }
            else
            {
                float playerDistance = (player.position - transform.position).magnitude;
                if (playerDistance < minRespawnDistance)
                {
                    StartRespawnTimer();
                    return;
                }
            }

            GameObject coinGameObject = Instantiate(
                coinPrefab,
                CoinPosition(),
                Quaternion.identity,
                transform) as GameObject;

            if (coinGameObject == null)
            {
                Debug.LogError("Failed to instantiate coin");
                return;
            }

            currentCoin = coinGameObject.GetComponentInChildren <CollectibleCoin>();
            if (currentCoin == null)
            {
                Debug.LogError("Can't spawn coin that doesn't have CollectibleCoin component");
                Destroy(currentCoin);
                return;
            }

            currentCoin.onCoinCollected = CoinCollected;
        }
Exemplo n.º 2
0
 private void CoinCollected(CollectibleCoin coin)
 {
     coinCollectedEvent.Invoke();
     StartRespawnTimer();
 }
Exemplo n.º 3
0
 public void StartRespawnTimer()
 {
     currentCoin = null;
     Invoke("GenerateCoin", spawnDelay);
 }