StartCoroutine in Collision unwork
Hi,everyone,I make a simply game for the Player touch by the Backteria will explosion, and Mission Fall Image will active , then Player will be destroy , after 5 second load GameOver scene ,but now the question is StartCoroutine(Do());
unwork , so I try to use Application.LoadLevel(LevelLoad);
straightly , but unwork , too . Does anyone have suggestions?
using UnityEngine;
using System.Collections;
public class EatPlayer : MonoBehaviour {
public GameObject explosion;
public GameObject loadingImage;
public string LevelLoad;
void OnCollisionEnter(Collision collider) {
if (collider.gameObject.name == "Backteria") {
StartCoroutine(Do());
Destroy(gameObject);
loadingImage.SetActive (true);
Instantiate(explosion, collider.transform.position, collider.transform.rotation);
}
}
IEnumerator Do () {
yield return new WaitForSeconds(5);
Debug.Log("hhhhhh");
Application.LoadLevel(LevelLoad);
}
}
Order of events:
Collide (Fire OnCollision)
Start Coroutine
Yield Coroutine (So not debug.log yet even)
Destroy Object (and everything on it including the script)
oblivion.
Answer by HenryStrattonFW · Nov 06, 2015 at 01:49 PM
@meat5000 's comment is accurate, the issue here is that you are destroying the object, long before the coroutine would ever get to the log. Coroutines still require the object to exist to continue, destroy the object and the coroutine goes with it.
Thanks for @meat5000 and @HenryStrattonFW point out the question , I use DontDestroyOnLoad
keep my Player won't be destroy ,then set WaitForSeconds(0.5f)
, change scene quickly, I know that not the perfect way to solve the problem, but I will keep trying .
Your answer
Follow this Question
Related Questions
Need to know why this script only works sometimes? 1 Answer
How to use a function from another script 1 Answer
multi verification 1 Answer
Detecting Collision is not working 1 Answer