Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Deekayz · Jan 08, 2016 at 06:10 AM · scripting problem

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);
         }
     }
 }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Deekayz · Jan 08, 2016 at 06:11 AM 0
Share

The last segment under void Update it what I need help with specifically.

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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";
         }
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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?

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Deekayz · Jan 08, 2016 at 07:12 PM 0
Share

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?

avatar image ClearRoseOfWar Deekayz · Jan 08, 2016 at 07:22 PM 0
Share

Sure :) Just place it in your Fixed Update.

place float seconds under your int count.

then you can remove your IEnumerator

avatar image Deekayz ClearRoseOfWar · Jan 08, 2016 at 07:41 PM 0
Share

Brilliant. I had to make a few adjustments but it works like a charm, thank you so much!

avatar image
0

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!

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Deekayz · Jan 08, 2016 at 05:16 PM 0
Share

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?

avatar image ZefanS Deekayz · Jan 11, 2016 at 01:34 AM 0
Share

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.

avatar image Deekayz · Jan 08, 2016 at 05:54 PM 0
Share

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);
             }
 }
avatar image Deekayz · Jan 08, 2016 at 06:03 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

41 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Trying to restrict movement to bounds of screen using MathF.Clamp() 2 Answers

Scene/Level changing problems 1 Answer

Why Collider2D size Y changes affects child object position 0 Answers

2D Projectile Script Not Working 1 Answer

How to Undo a lot of created objects at once? [Solved] 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges