Why the StartCoroutine(GameOver()); don't work
Why the StartCoroutine(GameOver()); don't work my code using System.Collections; using System.Collections.Generic; using UnityEngine.SceneManagement; using UnityEngine.UI; using UnityEngine;
public class P_Collide : MonoBehaviour {
public int Moneys;
public GameObject MoneyUI;
public static int health = 3;
public AudioClip CollectMoney;
public AudioClip pain;
// Update is called once per frame
void Update () {
if (health <= 0)
{
StartCoroutine(GameOver());
}
}
private void OnTriggerEnter2D(Collider2D trig)
{
if (trig.gameObject.tag == "Money")
{
GetComponent<AudioSource>().PlayOneShot(CollectMoney, 0.5f);
Moneys++;
MoneyUI.GetComponent<Text>().text = (Moneys.ToString());
Destroy(trig.gameObject);
}
if (trig.gameObject.tag == "log")
{
TakeDamage();
}
}
void TakeDamage()
{
GetComponent<AudioSource>().PlayOneShot(pain, 0.5f);
health--;
}
IEnumerable GameOver ()
{
yield return new WaitForSeconds(1.0f);
SceneManager.LoadScene("Main");
yield return null;
}
}
Answer by Komayo · Aug 05, 2017 at 03:07 PM
Also note:
IEnumerator GameOver() { // code here }
not work i had a error on GameOver() after the IEnumerator
Dunno if space before the () makes the error happen, i am away from ID$$anonymous$$
Please check if you have GameOver() or GameOver (), first is the right one.
Answer by Header-Mohammed · Aug 11, 2017 at 06:50 AM
i have now this error it is same this proplem with a another proplem in float
using System.Collections; using System.Collections.Generic; 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);
}
}
Answer by Alyousef · Aug 08, 2017 at 03:06 PM
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);
}
}