Help: Load next level 7 seconds after pick up count reaches 80
I have my code written out and it successfully takes me to the next level immediately after obtaining the 80th "pick up". However, I need it to wait at least 7 seconds before it loads the next level so that my win text can perform it's animation. Can anybody help me finish this code so that I'm able to do this?
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float speed;
public Text countText;
public Text winText;
public AudioClip impact;
AudioSource audio;
private Rigidbody rb;
private int count;
private float startTime;
void Start ()
{
audio = GetComponent<AudioSource>();
rb = GetComponent<Rigidbody> ();
count = 0;
SetCountText ();
winText.text = "";
startTime = Time.time;
}
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.AddForce (movement * speed);
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag ("Pick Up"))
{
other.gameObject.SetActive (false);
count = count + 1;
SetCountText ();
}
}
void SetCountText ()
{
countText.text = "Applications: " + count.ToString ();
if (count >= 80) {
winText.text = "You're Hired!";
audio.PlayOneShot (impact, 1.5F);
float totalTime = Time.time - startTime;
winText.text = "You're Hired!\n\nTime: " + totalTime.ToString("f1") + "s";
}
}
void Update ()
{
if (count >= 80) {;
Application.LoadLevel (1);
}
}
}
The last segment under void Update it what I need help with specifically.
Answer by Deekayz · Jan 08, 2016 at 07:46 PM
After compiling advised codes and making the changes needed, here is my working script:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float speed;
public Text countText;
public Text winText;
public AudioClip impact;
AudioSource audio;
private Rigidbody rb;
private int count;
private float seconds; //NEW CODE
private float startTime;
void Start ()
{
audio = GetComponent<AudioSource>();
rb = GetComponent<Rigidbody> ();
count = 0;
SetCountText ();
winText.text = "";
startTime = Time.time;
}
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.AddForce (movement * speed);
if (count >= 80) { //NEW CODE
seconds += Time.deltaTime; //NEW CODE
} //NEW CODE
if (seconds > 7) { //NEW CODE
Application.LoadLevel (1); //NEW CODE
} //NEW CODE
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag ("Pick Up"))
{
other.gameObject.SetActive (false);
count = count + 1;
SetCountText ();
}
}
void SetCountText ()
{
countText.text = "Applications: " + count.ToString ();
if (count >= 80) {
winText.text = "You're Hired!";
audio.PlayOneShot (impact, 1.5F);
float totalTime = Time.time - startTime;
winText.text = "You're Hired!\n\nTime: " + totalTime.ToString("f1") + "s";
}
}
}
Answer by ClearRoseOfWar · Jan 08, 2016 at 06:41 PM
float seconds;
if(count >= 80){
seconds += Time.deltaTime;
if(seconds > 7) //load next level
}
Maybe this?
Hey, thanks for answering! I'm terrible with coding, do you think you could show me how/where to implement your code into the above mentioned code?
Sure :) Just place it in your Fixed Update.
place float seconds under your int count.
then you can remove your IEnumerator
Brilliant. I had to make a few adjustments but it works like a charm, thank you so much!
Answer by ZefanS · Jan 08, 2016 at 06:58 AM
Here's one way of doing it:
function Update()
{
if (count >= 80)
{
NextLevel();
}
}
function NextLevel()
{
yield WaitForSeconds(7);
Application.LoadLevel(1);
}
Here's a link to the documentation.
Hope this helps!
Thanks for responding! So I'm getting this error in the yield line (also, the yield is red):
Assets/Scripts/PlayerController.cs(68,37): error CS1525: Unexpected symbol (', expecting
)', ,',
;', [', or
='
Does something else need to be added to get rid of this error?
Sorry, I was being silly and got mixed up between C# and Javascript. It's corrected now, but it seems like you've found a decent solution anyway.
Okay so now I have the game wait 7 seconds before loading the next level successfully, however I need it to start this function after I have picked up 80 pick ups. Any suggestions? Here's my script:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class PlayerController : $$anonymous$$onoBehaviour {
public float speed;
public Text countText;
public Text winText;
public AudioClip impact;
AudioSource audio;
private Rigidbody rb;
private int count;
private float startTime;
void Start ()
{
audio = GetComponent<AudioSource>();
rb = GetComponent<Rigidbody> ();
count = 0;
SetCountText ();
winText.text = "";
startTime = Time.time;
StartCoroutine (WaitAndLoad (7.0F));
}
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.AddForce (movement * speed);
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag ("Pick Up"))
{
other.gameObject.SetActive (false);
count = count + 1;
SetCountText ();
}
}
void SetCountText ()
{
countText.text = "Applications: " + count.ToString ();
if (count >= 80) {
winText.text = "You're Hired!";
audio.PlayOneShot (impact, 1.5F);
float totalTime = Time.time - startTime;
winText.text = "You're Hired!\n\nTime: " + totalTime.ToString("f1") + "s";
}
}
IEnumerator WaitAndLoad(float waitTime) {
yield return new WaitForSeconds (waitTime);
Application.LoadLevel (1);
}
}
I tried this, which I thought would work, but it didn't:
IEnumerator WaitAndLoad(float waitTime) {
if (count >= 80) {
yield return new WaitForSeconds (waitTime);
Application.LoadLevel (1);
}
}
Your answer
