• 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
3
Question by softarn · Jul 27, 2011 at 02:08 PM · yieldstartcoroutine

Gets stuck on WaitForSeconds

I'm having a problem with Coroutines and yield. When the ball passes the goal lines the score updates and then a new ball i supposed to be spawned after 1.5 sec. But I get stuck on yield return new WaitForSeconds(1.5f); in Pong.cs. I've read the docs but I can still not figure out whats the problem.

The score is added but no ball is spawned.

Updates:

  • If I comment out yield return new WaitForSeconds(1.5f); the code works, but of course doesn't wait.

  • I'm new to Unity and C# so don't trust my code as it was your own :)

Ball.cs attached to BallPrefab

 using UnityEngine;
 using System.Collections;
 
 public class Ball : MonoBehaviour {
     
     private static int TOP, BOT;
     private static Pong PONG;
     
     public float maxVelocity = 3;
     public float minVelocity = 0.1f;
     
     void Awake () {
         PONG = Pong.INSTANCE;
         TOP = ((int)Pong.TOP.z) + 2;
         BOT = ((int)Pong.BOT.z) - 2;
         rigidbody.velocity = new Vector3(0, 0, -7);
     }
     
     // Update is called once per frame
     void Update () {
         
         //Make sure we stay between the MAX and MIN speed.
         float totalVelocity = Vector3.Magnitude(rigidbody.velocity);
         if(totalVelocity>maxVelocity) {
             float tooHard = totalVelocity / maxVelocity;
             rigidbody.velocity /= tooHard;
         } else if (totalVelocity < minVelocity) {
             float tooSlowRate = totalVelocity / minVelocity;
             rigidbody.velocity /= tooSlowRate;
         }
         
         if(transform.position.z <= BOT) {
             Destroy(gameObject);
             StartCoroutine(PONG.Goal(1));
         } else if(transform.position.z >= TOP) {
             Destroy(gameObject);
             StartCoroutine(PONG.Goal(2));
         }
     }
 }

Pong.cs attached to MainCamera

 using UnityEngine;
 using System.Collections;
 using System.Threading;
 using System.Diagnostics;
 
 public enum PongGameState { playing, won, lost };
 
 public class Pong : MonoBehaviour {
     
     public Transform ballPrefab;
     public static Pong INSTANCE;
     
     public static Vector3 TOP = new Vector3(10, 0, 15);
     public static Vector3 BOT = new Vector3(10, 0, 5);
     public static Vector3 LEFT = new Vector3(5, 0, 10);
     public static Vector3 RIGHT = new Vector3(15, 0, 10);
     
     private PongGameState gameState;
     
     void Awake() {
         INSTANCE = t$$anonymous$$s;
         gameState = PongGameState.playing;
         Time.timeScale = 1.0f;
         SpawnBall();
     }
     
     private void SpawnBall() {
         print("spawnball");
         Instantiate(ballPrefab, new Vector3(10.0f, 0.0f, 10.0f), Quaternion.identity);
     }
     
     public IEnumerator Goal(int player){
         print("in goal");
         Scores.AddPoint(player);
         yield return new WaitForSeconds(1.5f);
         print("in goal");
         SpawnBall();
     }
 }
Comment
Add comment · Show 4
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 softarn · Jul 27, 2011 at 02:27 PM 2
Share
avatar image almo · Jul 27, 2011 at 02:29 PM 0
Share
avatar image almo · Jul 27, 2011 at 02:32 PM 0
Share
avatar image softarn · Jul 27, 2011 at 03:05 PM 0
Share

2 Replies

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

Answer by Mike 3 · Jul 27, 2011 at 03:27 PM

You're destroying the object w$$anonymous$$ch starts the coroutine, stopping it from running after the wait

Change t$$anonymous$$s line (both times):

 StartCoroutine(PONG.Goal(1));

to

 PONG.StartCoroutine(PONG.Goal(1));

So that the coroutine is run on the same object it's interested in

Edit, to make it more obvious why:

When you start a coroutine, the monobehaviour it's run from is "attached" to it in a way that when the monobehaviour is disabled or destroyed, the coroutine will stop running

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 cfbevan · Jul 27, 2011 at 02:47 PM

Try making Goal() return SpawnBall().

EDIT: Also check t$$anonymous$$s post form more information.

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 Mike 3 · Jul 27, 2011 at 03:30 PM 0
Share

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

7 People are following this question.

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

Related Questions

Fire rate for gun script, c# (not u/s) 1 Answer

Another question about yield, WaitForSeconds in c# 2 Answers

C# How to ping servers and get server latency? 1 Answer

"yield return StartCoroutine" in Start function 1 Answer

new Thread vs StartCoroutine 2 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