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 SquirrelCoder · Jan 11, 2016 at 04:52 PM · c#delay

How to delay a function?

I am making a Pong clone right now and I'm making the score ATM. What's supposed to happen is when the ball hits the left or right wall, it gives the player a point (just like the real game).

I want a delay before I reset the level but I can't figure out how to do it. I'm using 2 scripts, here's the first:

using UnityEngine; using UnityEngine.UI; using System.Collections;

public class GameManager : MonoBehaviour {

 public Text rScore;
 public Text lScore;
 public Text roundWinner;


 public GameObject ball;
 public GameObject ballSpawn;


 public int countR;
 public int countL;

 void Start () 
 {
     countR = 0;
     countL = 0;
     roundWinner.text = "";
     SetCountText ();
     Reset ();
 }

 public void SetCountText()
 {
     rScore.text = "" + countR.ToString ();
     lScore.text = "" + countL.ToString ();
 }

 public void Reset()
 {
     Instantiate (ball, ballSpawn.transform.position, Quaternion.identity);
     roundWinner.text = "";
 }

}

Here's the second:

using UnityEngine; using System.Collections;

public class Ball : MonoBehaviour { public float speed = 30;

 public GameManager gm;

 void Start () 
 {
     GetComponent<Rigidbody2D> ().velocity = Vector2.right * speed;
     gm = GameObject.Find ("GM").GetComponent<GameManager> ();
 }

 void OnCollisionEnter2D (Collision2D col)
 {
     if(col.gameObject.name == "RacketLeft")
     {
         float y = hitFactor(transform.position, col.transform.position, col.collider.bounds.size.y);

         Vector2 dir = new Vector2 (1, y).normalized;

         GetComponent<Rigidbody2D>().velocity = dir * speed;
     }

     if (col.gameObject.name == "RacketRight") 
     {
         float y = hitFactor(transform.position, col.transform.position, col.collider.bounds.size.y);

         Vector2 dir = new Vector2 (-1, y).normalized;
         
         GetComponent<Rigidbody2D>().velocity = dir * speed;
     }
 }

 float hitFactor(Vector2 ballPos, Vector2 racketPos, float racketHeight)
 {
     return(ballPos.y - racketPos.y) / racketHeight;
 }

 void OnTriggerEnter2D (Collider2D other)
 {
     if(other.gameObject.CompareTag("WallR"))
     {
         gm.roundWinner.text = "Round Winner: Left";
         gm.countL += 1;
         gm.SetCountText();
         Destroy(gameObject, 1);
         //I want to call the Reset function here
     }

     if(other.gameObject.CompareTag("WallL"))
     {
         gm.roundWinner.text = "Round Winner: Right";
         gm.countR += 1;
         gm.SetCountText();
         Destroy(gameObject, 1);
         //I want to call the Reset function here
     }
 }

}

Thanks in advance :)

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

Answer by UniqueMAX · Jan 11, 2016 at 05:19 PM

Use Coroutine. (add it anywhere like another void)

 IEnumerator Reset(float Count){
 yield return new WaitForSeconds(Count); //Count is the amount of time in seconds that you want to wait.
 //And here goes your method of resetting the game...
 yield return null;
 }

Add a public variable that would allow you to modify the time it takes before resetting the game without editing script.

 public float WaitTime=1.0f;

And finally, add this to places where you want to call the Reset function

 StartCoroutine("Reset", WaitTime);

I hope it helps, I think it's a nice and simple way of doing it =)

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 JacobGames · Feb 17, 2016 at 05:13 PM

@SquirrelCoder there's a recently released asset that can help you delay anything in one line of cod. No need to use multiple lines of code as with coroutines.

It's called Super Invoke, it's like a more powerful version of the standard Invoke function of MonoBehavior.

You can also create sequences with methods and delays which will actually be useful in your case.

Your code using Super Invoke would be:

 if(other.gameObject.CompareTag("WallL"))
  {
      gm.roundWinner.text = "Round Winner: Right";
      gm.countR += 1;
      gm.SetCountText();
      
      SuperInvoke.CreateSequence()
                 .AddMethod( ()=> Destroy(gameObject), 1)
                 .AddMethod( ()=> gm.Reset())
                 .Run();
  }

The Reset function will be called after Destroy.

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 kayb14 · Jun 21, 2016 at 07:46 AM

I am tired of coding delay functions everywhere I need to!

So I wrote a solution to delay ANYTHING!!!!

If you have some space in your performance, because it's very dirty!

  1. create a cube outside of the map/player sight and remove its mesh-renderer and collider! should work with an empty object as well....

public GameObject timedelay; D&D the aforementioned cube in here

2 . Create a "destroy "script that destroys that cube after a delay (in this case 5seconds)

 bool destroyed = false;
 
 public void DestroyObject ()
 {destroyed=true;}
 
 update()
 {
 if(destroyed==true)
 {Destroy(gameObject,5);}
 }


3 . run the script where you want to put the reset

 if ("YOURCONDITIONS"&&timedelay!=null){timedelay.transform.GetComponent<destroy>(DestroyObject)}


4 . Check if the cube is destroyed & trigger RESET()

  else if ("YOURCONDITIONS"&&timedelay == null){"TRIGGER-RESET-FUNCTION"}
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 thanasiskap · Sep 03, 2018 at 03:03 PM 0
Share

I'm afraid this is not a good solution.

Checking every frame on Update() just to call a delayed function after 5 seconds? Why would anyone ever want to do that?

You can just call your delayed function straight away

Check file TimedObjectDestructor.cs in the official Unity Standard Assets in the Utility package.

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

7 People are following this question.

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

Related Questions

Recursive Functions with Delay 0 Answers

Unity2D I wanna use a delay time on Entering an Trigger. 0 Answers

Make my moving platforms pause at its endpoints using c# 2 Answers

How can i change the value of a variable from an IEnumerator 0 Answers

Are Coroutines the best way to delay ? If not, what is ? C# 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