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 /
avatar image
1
Question by ByNeo1000 · Apr 10, 2016 at 06:28 PM · c#coroutinewaitforsecondswithout

How can i wait seconds without a coroutine (C#)?

I'm don't want to use a coroutine, because this is repeating a lot of times and i only need one time.

This is the code: using UnityEngine; using System.Collections;

 public class AtaqueMeleeSkeleton: MonoBehaviour {
 
     public GameObject target;
     public float tiempoDeAtaque;
     public float cooldDown;
     public int DañoSkeleton;
 
     Animator animator;
 
 
     // Use this for initialization
     void Start () {
         target = GameObject.FindWithTag("Player");
 
         animator = GetComponent<Animator>();
 
         tiempoDeAtaque = 0;
         cooldDown = 5.0F;
 
     }
 
     // Update is called once per frame
     void Update () {
 
         if (tiempoDeAtaque > 0)
             tiempoDeAtaque -= Time.deltaTime;
 
         if (tiempoDeAtaque < 0)
             tiempoDeAtaque = 0;
 
         if (tiempoDeAtaque == 0) {
             Attack ();
         }
 
 
     }
 
     void Attack (){    
 
         float distance = Vector3.Distance (target.transform.position, transform.position);
 
         Vector3 dir = (target.transform.position - transform.position).normalized;
 
         float direction = Vector3.Dot (dir, transform.forward);
 
         //Debug.Log (direction);
         //Debug.Log (distance);
 
         if (distance < 2.5) {
             if (direction > 0) {
 
                 StartCoroutine (restarVida ());
                 StopCoroutine (restarVida ());
             }
         }
         else {
             tiempoDeAtaque = 0;
         }
     }
 
 
     IEnumerator restarVida (){
 
         if (Vector3.Distance (target.transform.position, transform.position) < 2.5) {
 
             if (Vector3.Dot ((target.transform.position - transform.position).normalized, transform.forward) > 0) {
 
                 animator.SetBool ("Attack", true);
 
                 yield return new WaitForSeconds (0.9f);
 
                 Debug.Log ("Attack");
                 EstadoDelPlayer vida = (EstadoDelPlayer)target.GetComponent ("EstadoDelPlayer");
                 vida.AjustarVidaActual (-DañoSkeleton);
 
                 animator.SetBool ("Attack", false);
 
                 tiempoDeAtaque = cooldDown;
 
             }
         }
     }
 }
 
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 fafase · Dec 28, 2018 at 10:39 AM 0
Share

you could simplify your Update:

  void Update () 
 {
      if(attacking == true){ return; }
      tiempoDeAtaque -= Time.deltaTime; 
      if (tiempoDeAtaque < 0)
      {
          Attack();
      }
  }

Then in Attack, set the attacking to true

4 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by FWCorey · Apr 23, 2016 at 02:14 AM

Justuse Invoke(method, delay).

http://docs.unity3d.com/ScriptReference/MonoBehaviour.Invoke.html

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 Happy-Zomby · Apr 10, 2016 at 06:44 PM

hi, 1/ you could use a coroutine but if you don't want to repeat you can do something like

 void Update ()
 {
 if(attack == true) 
         {
         attack = false;
         Attack ();
         WaitUntilReloaded();
         }
 }
 
 
 void WaitUntilReloaded()
 {
 yield WaitForSeconds(ReloadTime);
 attack = true;
 }

2/ if it is systematically attacking you could use an invoke repeating http://docs.unity3d.com/ScriptReference/MonoBehaviour.InvokeRepeating.html

hope that helps

Comment
Add comment · Show 10 · 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 ByNeo1000 · Apr 10, 2016 at 07:16 PM 0
Share

The line 14 it doesn't working.

avatar image Happy-Zomby ByNeo1000 · Apr 10, 2016 at 07:29 PM 0
Share

my bad - that was unity js - I'm in the process of shifting to c#

for the yield in c# - you can see here: http://answers.unity3d.com/questions/350721/c-yield-waitforseconds.html

avatar image ByNeo1000 Happy-Zomby · Apr 10, 2016 at 07:36 PM 0
Share

But i don't want to use coroutines, because is always repeating.

Show more comments
avatar image Oribow ByNeo1000 · Apr 10, 2016 at 07:31 PM 0
Share

Would be: yield return new WaitForSeconds(ReloadTime);

avatar image ByNeo1000 Oribow · Apr 10, 2016 at 07:34 PM 1
Share

Yes, this is the correct form, but is a void not a coroutine then WaitForSeconds can not be able.

Show more comments
avatar image fafase · Dec 28, 2018 at 10:37 AM 1
Share

This cannot work, yield WaitForSeconds(ReloadTime); or yield new WaitForSeconds(ReloadTime);requires a coroutine in Unity.

avatar image gnostici · Jun 10, 2020 at 01:13 AM 0
Share

"How can I wait without a coroutine?" "You can use a coroutine."

Well, thanks.

avatar image
1

Answer by Oribow · Apr 10, 2016 at 06:46 PM

You can check in the update function if enough time has passed. Set StartTime to Time.time when you want the timer to start and set timerStart to true.

 float startTime = 0;
 float waitFor = 4;
 bool timerStart = false;
 void Update()
 {
     if (timerStart && Time.time - startTime > waitFor)
     {
         //Do something
        timerStart = false;
     }
 }
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 gvergidis · Dec 27, 2018 at 07:57 PM

Hello.

I read that you do not like to use Coroutine but I am doing something like this. I have a helper script with assisting functions and one of them is :

 public void DelayedExecute(float delay, Action onExecute)
 {
     // Check if delay is valid.
     if (delay < 0) return;
 
     StartCoroutine(DelayRoutine(delay, onExecute));
 }
 
 private IEnumerator(float delay, Action onExecute)
 {
     // Wait for given delay
     yield return new WaitForSeconds(delay);
 
     onExecute?.invoke();
 }


So using this is extreme easy. Just do this :

 DelayedExecute(2f, () =>
 {
     Debug.LogError("I was printed 2 seconds later...");
 });

You can further edit this method to parse parameters. Maybe parsing a list of object params.

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.

Follow this Question

Answers Answers and Comments

9 People are following this question.

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

Related Questions

Rotate an object using Quaternion.RotateTowards() inside a Coroutine? 1 Answer

C# simple delay execution without coroutine? 2 Answers

Can't get past WaitForSeconds in my coroutine 1 Answer

WaitForSeconds not working C# 1 Answer

How to get precise spawn interval (C#)? 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