Question by
Bleackk · Mar 18, 2016 at 06:43 PM ·
c#uiscene-loadingwaitforseconds
Scene Change after some time when achieving a certain Score C#
Hello everyone, i'm making a simple game where you gather blocks, each block = 1 to score. It's based on the " Roll a Ball " Tutorial. Now here's what i want to do.
I need to change the scene after let's say 5 seconds using " WaitForSeconds " so i can display a " Good Job " Text and Change the Game Scene to the Next level after those 5 seconds.
Here's the script I wrote, i used a " private bool counting = false; " to check if the score was reached.
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using System.Collections;
public class PlayerControlls : MonoBehaviour {
private Rigidbody rb;
private int count;
public float speed;
public float waitTime;
public Text countText;
public Text winText;
private bool counting = false;
void Start ()
{
StartCoroutine (changeDelay ());
rb = GetComponent<Rigidbody> ();
count = 0;
SetCountText ();
winText.text = "";
}
void FixedUpdate ()
{
float mH = Input.GetAxis ("Horizontal");
float mV = Input.GetAxis ("Vertical");
Vector3 move = new Vector3 (mH, 0, mV);
rb.AddForce (move * speed * Time.deltaTime);
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag ("Pick Up"))
{
other.gameObject.SetActive (false);
count = count + 1;
SetCountText ();
}
}
void Update()
{
if (counting)
{
changeDelay ();
SceneManager.LoadScene ("Level2");
}
}
IEnumerator changeDelay ()
{
yield return new WaitForSeconds (10);
}
void SetCountText()
{
countText.text = "Score: " + count.ToString();
if (count >= 1 && !counting)
{
counting = true;
winText.text = "Good Job";
}
}
}
ANY help would be apreciated. Unity is updated to the latest STABLE version, so 5.3.4
Comment
Best Answer
Answer by Bleackk · Mar 18, 2016 at 07:02 PM
Allready fiured it Out. Here's the difference :) For anyone looking for the same thing
IEnumerator changeDelay ()
{
SetCountText ();
yield return new WaitForSeconds (10);
SceneManager.LoadScene ("Level2");
}
void SetCountText()
{
countText.text = "Score: " + count.ToString();
if (count >= 1 && !counting)
{
counting = true;
winText.text = "Good Job";
}
}
}
Instead of Invoking the IEnumerator in SetCountText i reversed the Process.