lol@timer thing, anyways bottom line is you cannot block update() thats called every frame so to ‘yield’ you have to write another function that returns IEnumerator which is nothing but a way of telling the compiler that “dude this bitch aint no regular method call, hold onto execution step where it yields so yea know where to start at in next frame”
IEnumerator Process()
{
//Wait 1 second
yield return StartCoroutine(Wait(1.0f));
//Do process stuff
}
IEnumerator Wait(float seconds)
{
yield return new WaitForSeconds(seconds);
}
All in all you can actually stop parts of update or the entire function. What people are failing to realize is that with a timer you could set a bool to freeze parts of update or sections put a check at the beginning of update to see if the bool needs to be changed back and bam you can stop parts of Update() just as easily as anything… since I really don’t like java script at all here is the C# version.
C#
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(AudioSource))]
public class Player_Movement_Sounds : MonoBehaviour
{
public AudioClip Walking;
public AudioClip Running;
public float Movement_Volume = 0.05f;
public float delay_set = 0.75f;
private float delay = 0.0f;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
Movement_Sounds();
}
void Movement_Sounds()
{
if(Input.GetKey(KeyCode.Space) || delay > 0.0f)
{
if(delay > 0.0f)delay -= Time.deltaTime;
else delay = delay_set;
audio.Stop();
}else
if(Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D))
{
if(Input.GetKey (KeyCode.LeftShift))
{
//running
if(!audio.isPlaying)
{
audio.clip = Running;
audio.volume = (Movement_Volume);
audio.Play();
}
}else
{
if(!audio.isPlaying)
{
audio.clip = Walking;
audio.volume = (Movement_Volume);
audio.Play();
}
}
}else
{
if(Input.GetKey(KeyCode.S)) //can't run backwards.
{
if(!audio.isPlaying)
{
audio.clip = Walking;
audio.volume = (Movement_Volume);
audio.Play();
}
}else
{
//not moving
audio.Stop();
}
}
}
}
[code]
None of these methods are working for me PLEASE HELP!!!
do you really expect anyone to be able to assist you given that you’ve provided absolutely nothing?
ask a specific detailed question providing information about what you have/want so the members of the forum have something to work with and you’ll find these forums invaluable. Necro old threads with nonsense nothing posts and you’ll get no where.
Your choice.
Honestly, I like to use InvokeRepeating.
It does not generate garbage unlike coroutines (built-in no addons).
As I do not know what your use case is this may not be ideal but the options is here if you want it.
https://docs.unity3d.com/ScriptReference/MonoBehaviour.InvokeRepeating.html
- You would start by creating a function which does what you would like to do.
void DoSomething()
{
// Do something here
Debug.Log("DoSomething has been called!");
}
- Either use Invoke or InvokeRepeating to call the function.
void Start()
{
// Use InvokeRepeating to start the function.
// InvokeRepeating calls the function every few seconds. (specified when calling InvokeRepeating)
// InvokeRepeating ("FunctionName", DelayTimeBeforeStart, RepeatEveryXSeconds)
InvokeRepeating("DoSomething", 1f, 1f);
// Alternatively if you do not want it to repeat you could use Invoke
// Invoke ("FunctionName", DelayTimeBeforeStart);
Invoke("DoSomething", 10f);
}
- You can then cancel the InvokeRepeating with CancelInvoke();
CancelInvoke();
Full Code:
void Start()
{
// Use InvokeRepeating to start the function.
// InvokeRepeating calls the function every few seconds. (specified when calling InvokeRepeating)
// InvokeRepeating ("FunctionName", DelayTimeBeforeStart, RepeatEveryXSeconds)
InvokeRepeating("DoSomething", 1f, 1f);
// Alternatively if you do not want it to repeat you could use Invoke
// Invoke ("FunctionName", DelayTimeBeforeStart);
Invoke("DoSomething", 10f);
}
void DoSomething()
{
// Do something here
Debug.Log("DoSomething has been called!");
}
void StopInvoke()
{
CancelInvoke();
}
DISCLAIMER:
If it is supposed to be running for a long time. (Full playtime span) Then that is what Coroutines are designed for.
I agree, the useless posts and begging for free code does get very annoying. However, I was there at a point, young and wanting to create games so I can sympathize a little. (Although not too much as it is extremely annoying)
For anyone that is concerned about performance, you can declare your WaitForSeconds so you’re not generating a new one every time
public WaitForSeconds 1SecPause = 1;
...
...
yield return 1SecPause;
I wouldn’t recommend the “infinite loop” method of waiting, because that’s going to block everything. Coroutines sound exactly like what you need here, but yes, all the functions that you want to be doing the waiting need to be coroutines.
So if you want to pause inside Update … don’t. Update is called every single frame, it doesn’t logically make sense to pause inside it. If you’re doing it in response to user input, then have it start a separate coroutine:
void Update () {
if(Input.GetKey(KeyCode.Space) {
StartCoroutine(Foo());
}
}
IEnumerator Foo () {
// Do something
yield return new WaitForSeconds(3f); // Wait three seconds
// Do something else
}
If you’re trying to implement logic like “the object moves forward for three seconds, then goes into reverse”, then either:
A) Use a coroutine / invoke instead of Update.
B) Keep some state variables which you refer to in Update.
So here they solved it perfectly:
Ok.
The 2 ways above have worked for me.
This:
void Update () {
if(Input.GetKey(KeyCode.Space) {
StartCoroutine(Foo());
}
}
IEnumerator Foo () {
// Do something
yield return new WaitForSeconds(3f); // Wait three seconds
// Do something else
}
And this:
void Start()
{
// Use InvokeRepeating to start the function.
// InvokeRepeating calls the function every few seconds. (specified when calling InvokeRepeating)
// InvokeRepeating ("FunctionName", DelayTimeBeforeStart, RepeatEveryXSeconds)
InvokeRepeating("DoSomething", 1f, 1f);
// Alternatively if you do not want it to repeat you could use Invoke
// Invoke ("FunctionName", DelayTimeBeforeStart);
Invoke("DoSomething", 10f);
}
void DoSomething()
{
// Do something here
Debug.Log("DoSomething has been called!");
}
Finally!!
I’ve always found reference about the IEnumerator/yield stuff, but it has never worked for me! This time it has! And the Invoke function where we can set a delay time to start i found awesome! Very easy!
I guess i was trying to implement the StartCoroutine function inside my OncollisionEnter(). When i placed it in the Update() it worked! ![]()
Thank you all!
While this is a very old thread…I just wanted to point something out to you. GetKey triggers while the user holds down the key. Just be careful you are getting the result you want. While holding down space for example, you will call Foo repeatedly, thus creating several calls to the coroutine that will all yield for 3 seconds before doing something. Just keep that in mind if that isn’t the result you wanted.
all this over one question XD Must Take A Lot Of Effort
Is there a way to stop it from doing that?
I tried using:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class Remains : MonoBehaviour
{
public GameObject remains;
/*
IEnumerator Foo()
{
yield return new WaitForSeconds(3f); // Wait three seconds
Destroy(remains);
}
*/
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown("space"))
{
Instantiate(remains, transform.position, Quaternion.identity);
//StartCoroutine(Foo());
Invoke("Foo", 4f);
Destroy(gameObject);
}
}
void Foo()
{
// Do something here
Debug.Log("DoSomething has been called!");
}
}
But it never calls the destroy part at all on the Instantiated cubes.
Two problems:
- You’re destroying the GameObject that has the Remains component on it, not the new one you instantiated.
- You’re doing it directly inside Update, not in Foo, so it would happen instantly after instantiating them.
Since you need to keep track of the instantiated remains, you either need to use a coroutine instead of Invoke, or else store a reference in a member field.
Neither coroutines or Invoke applies any delay to code that’s on the next line:
void Foo () {
Debug.Log("This happens immediately.");
Invoke("Bar", 3f);
Debug.Log("This also happens immediately.");
}
void Bar () {
Debug.Log("THIS happens after three seconds.");
}
But yield statements inside a coroutine do:
IEnumerator Foo () {
Debug.Log("This happens immediately.");
yield return new WaitForSeconds(3f);
Debug.Log("This happens after three seconds.");
}
Thanks!
Are there any function(wait a number of seconds) that works in EditorWindow ? I mean not in MonoBehaviour.
is there a function similar to yield wait for seconds in update window?
no, you just have to call the coroutine in start and it should continue to be called…?
(also I do know this is old, kind of just checking my knowledge and incorrect terminology)
yes, just what u needed