Question by
kobeshelby04 · Mar 03, 2020 at 12:24 AM ·
2d-gameplayplayer-settings
My IEnumerator method is not being called in the if statement when player lives are equal to zero. Help!
public class Destroyedplayer : MonoBehaviour
{
public GameObject Gameover;
public Text LiveUITexts;
public AudioSource mAudioScr;
public GameObject ExplosionGo;
private IEnumerator WaitForSceneLoad() {
while (true)
{
if (Gameover.gameObject == true)
yield return new WaitForSceneLoad(3);
SceneManager.LoadScene("Title Screen");
yield return null;
}
}
int lives = 3;
public void Int()
{
const int MaxLives = 3;
lives = MaxLives;
LiveUITexts.text = lives.ToString();
}
void Start()
{
Gameover.gameObject.SetActive(false);
}
public void Update()
{
if (lives == 0)
{
Destroy(gameObject);
Gameover.gameObject.SetActive(true);
StartCoroutine(WaitForSceneLoad());
}
}
void OnTriggerEnter2D(Collider2D col)
{
mAudioScr = GetComponent<AudioSource>();
if ((col.tag == "EnemyTag") || (col.tag == "EnemyBulletTag"))
{
lives--;
mAudioScr.Play();
LiveUITexts.text = lives.ToString();
PlayExplosion();
}
}
void PlayExplosion()
{
GameObject explosion = (GameObject)Instantiate(ExplosionGo);
explosion.transform.position = transform.position;
}
}
Comment
Best Answer
Answer by tormentoarmagedoom · Mar 03, 2020 at 05:03 PM
Hello.
Of course not. Well actually is calling it, but you destory the object 2 lines before so all the monobehaviour stops at the end of the frame.
Your answer
Follow this Question
Related Questions
Unity 2D 5.2.2 How can I aim with keys towards an object? 2 Answers
2d platformer physics 0 Answers
How to merge a 2D Quiz with a 3D FPS? 0 Answers
Getting rid of 2D collision jitter 1 Answer
Is it possible to add an Input Counter during Splash Screens? 0 Answers