• 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 Eddie_Reilly · Dec 30, 2014 at 09:36 PM · waitforsecondsquit

WaitForSeconds not working c#

Hello I am quite new to programming in unity.

I am making a pong clone.

I have set up a script that adds 1 to the score of the player on the opposing side of the board when the ball enters a trigger zone and resets its position.

However my Waitforseconds part of the code will not work. The ball just keeps moving due to the function I call after the wait for seconds code. If I take out the function the ball stops moving and just sits there. So I know that it is the Waitforseconds part.

I cannot find any solutions to this problem.

Here is my code:

 using UnityEngine;
 using System.Collections;
 
 public class scorecounter : MonoBehaviour {
 
 
     public Transform leftWall;
     public Transform rightWall;
     public float playerscore = 0f;
     public float aiscore = 0f;
     public float speed = 50f;
 
     // Use this for initialization
     void Start () {
     
     }
 
 
 
 
     IEnumerator hi (float secs){
         print (Time.time);
         yield return new WaitForSeconds (secs);
         print (Time.time);
     }
 
 
     void ResetBall() {
         var vel = rigidbody.velocity;
         vel.x = 0;
         vel.y = 0;
         rigidbody.velocity = vel;
         gameObject.transform.position = new Vector3 (0.14f, 0.5f, 0.0f);
 }
     void GoNow(){
         rigidbody.AddForce (new Vector3 (20.0f, 15.0f) * speed);
     }
     
     void OnTriggerEnter(Collider other){
         if (other.gameObject.name == "leftWall") {
             aiscore += 1;
             Debug.Log(aiscore);
             ResetBall();
             StartCoroutine(hi(2.0f));
             GoNow();
 
     }
 
         if (other.gameObject.name == "rightWall") {
             playerscore += 1;
             Debug.Log(playerscore);
             ResetBall();
             StartCoroutine(hi(2.0f));
             GoNow();
 
         }
 
 
 
     }
 
         
     // Update is called once per frame
     void Update () {
     
     }
 }

Comment
Add comment
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

3 Replies

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

Answer by supernat · Dec 30, 2014 at 09:39 PM

Move GoNow() into the hi routine, at the end of it. When you call StartCoroutine(), it doesn't execute the method "hi" completely, it just puts it on the stack of parallel execution methods and then iterates to the first yield statement at which point it returns from start coroutine and runs GoNow.

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 Eddie_Reilly · Dec 30, 2014 at 10:12 PM 0
Share

so do this ?

 IEnumerator hi (float secs){
 print (Time.time);
 yield return new WaitForSeconds (secs);
 print (Time.time);
 GoNow();
 }

avatar image
1

Answer by UnityKen · Dec 30, 2014 at 10:03 PM

I might be wrong but shouldn't secs be a number?

      IEnumerator hi (float secs){
          print (Time.time);
          yield return new WaitForSeconds (secs);
          print (Time.time);
      }
 
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
0

Answer by EggQuiz857 · Dec 30, 2014 at 10:05 PM

 yield return new WaitForSeconts (num);

Try it out

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

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to end a game with ESC button. 1 Answer

Stop Unity Play 1 Answer

Script won't exit application.Why? 1 Answer

Unity 5 - ExitApplication(); causes Crash 1 Answer

Saving highscore with PlayerPrefs 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges