Question by
Alyousef · Aug 11, 2017 at 06:50 AM ·
c#scripting problembeginnerfloatstartcoroutine
why StartCoroutine(SpawnBigTree()); don't work and the float has a error
why StartCoroutine(SpawnBigTree()); don't work and the float has a error
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*;*****************************
// 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);
}
IEnumerable 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(15.0f, 45.0f);
}
Debug.Log("Complete");
yield return new WaitForSeconds(1);
Instantiate(BigTree, spawnSpot, Quaternion.identity);
BigTreeTimer = Random.Range(15.0f, 45.0f);
}
}
Comment