Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by hashish · Jul 08, 2014 at 07:10 AM · coroutineyielddelay

restart level when dead with delay

Hello guys, i'm struggling with something. after my player loses all his lives, i want him to restart the level with a delay i did add a yield but i keep getting an error i don't know what to do

this is the error

 Assets/Scripts/PlayerController.cs(56,14): error CS1624: The body of `PlayerController.SetCountText()' cannot be an iterator block because `void' is not an iterator interface type
 

and here is my script

 using UnityEngine;
 using System.Collections;
 
 public class PlayerController : MonoBehaviour
 {
 
     public float speed;
     public GUIText countText;
     public GUIText winText;
     public GUIText LiveText;
     public GUIText LoseText;
     public AudioClip LoseAudio;
     public AudioClip LiveSound;
     public AudioClip PickUpSound;
     public AudioClip WinAudio; 
     private int PickUpCount;
     public int LiveCount;
 
 
     void start ()
     {
         PickUpCount = 0;
         LiveCount = 3;
         SetCountText ();
         winText.text = "";
     }
 
     void FixedUpdate ()
     {
         float moveHorizontal = Input.GetAxis("Horizontal");
         float moveVertical = Input.GetAxis("Vertical");
         
         Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
         
         rigidbody.AddForce(movement * speed * Time.deltaTime);
     }
 
     void OnTriggerEnter(Collider other)
     {
         if(other.gameObject.tag == "PickUp")
         {
             AudioSource.PlayClipAtPoint(PickUpSound, new Vector3(5, 2, 2));
             other.gameObject.SetActive(false);
             PickUpCount = PickUpCount + 1;
             SetCountText ();
         }
         if(other.gameObject.tag == "Obstacle")
         {
             AudioSource.PlayClipAtPoint(LiveSound, new Vector3(5, 2, 2));
             other.gameObject.SetActive(false);
             LiveCount = LiveCount - 1;
             SetCountText ();
         }
     }
     
     void SetCountText()
     {
             countText.text = "Blocks: " + PickUpCount.ToString ();
             if (PickUpCount >= 16) {
                     AudioSource.PlayClipAtPoint (WinAudio, new Vector3 (5, 2, 2));
                     winText.text = ("WE MADE IT!");
             }    
             {
             LiveText.text = "Lives: " + LiveCount.ToString ();
             if (LiveCount == 0) {    
                     AudioSource.PlayClipAtPoint (LoseAudio, new Vector3 (5, 2, 2));
                     LoseText.text = ("TRY AGAIN BRO");
                     LiveCount = 0;
                     yield return new WaitForSeconds(5f);
                     Application.LoadLevel("level 2");
             }        
             }
     }    
 }
 
 

i'd love to get some help here

Comment
Add comment · Show 6
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 meat5000 ♦ · Jul 08, 2014 at 01:47 AM 3
Share

http://docs.unity3d.com/$$anonymous$$anual/Coroutines.html

avatar image HarshadK · Jul 09, 2014 at 02:00 PM 0
Share

It's better if you specify why it doesn't help so we can tell how to make it work or provide another way. :-)

avatar image hashish · Jul 09, 2014 at 02:04 PM 0
Share

what would really help is to give me an example! thanks for the quick reply tho

avatar image HarshadK · Jul 09, 2014 at 02:10 PM 0
Share

Haven't read your code but I can tell you the basic logic as an example.

 bool isPlayerDead;
 
 if(isPlayerDead) {
      StartCoroutine(RestartLevel);
 }
 
 IEnumerator RestartLevel() {
     yield return new WaitForSeconds(timeToWait);
     Application.LoadLevel(Application.loadedLevel);
 }

avatar image hashish · Jul 09, 2014 at 02:12 PM 0
Share

can you also tell me what's wrong with my code? it's killing me lol

Show more comments

1 Reply

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

Answer by HarshadK · Jul 09, 2014 at 02:21 PM

 if (LiveCount == 0) {   
     AudioSource.PlayClipAtPoint (LoseAudio, new Vector3 (5, 2, 2));
     LoseText.text = ("TRY AGAIN BRO");
     LiveCount = 0;
     StartCoroutine(RestartLevel);
 }
 
 IEnumerator RestartLevel() {
     yield return new WaitForSeconds(5f);
     Application.LoadLevel("level 2");
 }

The basic reason you are getting that error is because you are making your coroutine of return type void which is not allowed. Usually coroutine should have a proper return type like IEnumerator.

But I've restructured your code to have a separate coroutine altogether. If you want you can make the type of your SetCountText() function as IEnumerator instead of void.

Comment
Add comment · Show 1 · 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 hashish · Jul 09, 2014 at 03:07 PM 0
Share

thanks! i figured out how to do it and i totally understand it now

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Inserting a delay 2 Answers

Coroutine not running after yield return new WaitForSeconds 3 Answers

WaitForSeconds Question 2 Answers

why does Coroutine 'yield return 0' sometimes take a lot of frames before it continues 1 Answer

Coroutoutine doesnt work properly. 3 Answers


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