Question by
Dimuburger · May 06, 2020 at 07:17 AM ·
2d gamebeginnerienumeratorload scene
How can i WaitForSeconds after destroying GameObject to load the next Scene?
Hey Unity Users, I am a beginner and try to make my first 2D Game. I would like to wait 3 seconds after the player has collected the item until the next scene loads. I don't know if the method I wrote can work that way. I did not find anything on the Internet or in the forum that could help me. If this post is in the wrong topic Sorry fot it.
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using UnityEngine;
using UnityEngine.SceneManagement;
public class TriforceScript : MonoBehaviour
{
public int score;
public AudioSource CollectSound;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
if (score == 0)
{
score++;
}
if (score > 0)
{
score += 10;
}
CollectSound.Play();
Destroy(gameObject);
loadScene();
}
IEnumerator loadScene()
{
yield return new WaitForSeconds(1f);
SceneManager.LoadScene(1);
}
}
}
Comment