This post has been wikified, any user with enough reputation can edit it.
why the Timer isn't working
why the Timer of depree it's between (0.005f, 0.1f); and he was so long what's the problem my code
using System.Collections; using UnityEngine;
public class Opject_Sp : MonoBehaviour {
public GameObject Player;
public GameObject Money;
public GameObject BigTree;
public GameObject[] Rocks;
public GameObject[] debrees;
private float MoneyTimer = 2.0f;
private float RockTimer = 2.0f;
private float BigTreeTimer = 5.0f;
public AudioClip BigRockFalling;
// Update is called once per frame
void Update()
{
MoneyTimer -= Time.deltaTime;
RockTimer -= Time.deltaTime;
BigTreeTimer -= Time.deltaTime;
if (MoneyTimer < 0)
{
SpawnMoney();
}
if (RockTimer < 0)
{
SpawnRock();
}
if (BigTreeTimer < 0)
{
StartCoroutine(SpawnBigTree());
}
}
void SpawnMoney()
{
GameObject Mon = Instantiate(Money, new Vector2(Random.Range(-6.47f, 6.62f), 6), Quaternion.identity) as GameObject;
MoneyTimer = Random.Range(0.5f, 2.5f);
}
void SpawnRock()
{
GameObject Roc = Instantiate(Rocks[(Random.Range(0, Rocks.Length))], new Vector2(Random.Range(-7f, 7f), 10), Quaternion.identity) as GameObject;
RockTimer = Random.Range(0.5f, 2.5f);
Roc.GetComponent<Rigidbody2D>().gravityScale = Random.Range(1, 3);
}
IEnumerator SpawnBigTree()
{
BigTreeTimer = 1000;
Vector2 spawnSpot;
int debreeTimer = (Random.Range(20, 30));
int spawnOnPlayerPer = (Random.Range(0, 100));
if (spawnOnPlayerPer < 50)
{
spawnSpot = new Vector2(Random.Range(-7f, 7f), 20);
}
else
{
spawnSpot = new Vector2(Player.transform.position.x, 20);
}
while (debreeTimer > 0)
{
debreeTimer--;
float t = Random.Range(0.005f, 0.1f);
yield return new WaitForSeconds(t);
Debug.Log("Spawned");
GameObject debree = Instantiate(debrees[(Random.Range(0, debrees.Length))], new Vector2(spawnSpot.x + Random.Range(-1.0f, 1.0f), spawnSpot.y -10), Quaternion.identity) as GameObject;
yield return new WaitForSeconds(1);
Instantiate(BigTree, spawnSpot, Quaternion.identity);
BigTreeTimer = Random.Range(30.0f, 45.0f);
}
Debug.Log("Complete");
yield return new WaitForSeconds(1);
Instantiate(BigTree, spawnSpot, Quaternion.identity);
Player.GetComponent<AudioSource>().PlayOneShot(BigRockFalling, 3.0f);
BigTreeTimer = Random.Range(30.0f, 45.0f);
}
}
Comment