#coroutine stuff
1 messages · Page 1 of 1 (latest)
For now just don’t use Thread.Sleep since it pauses the entire editor. The proper way to pause a coroutine is using yield return new WaitForSeconds(5f); Here’s an example:
StartCoroutine(Hello());
IEnumerator Hello(){
Debug.Log(“Hello now”);
yield return new WaitForSeconds(5f);
Debug.Log(“Hello 5 seconds later!”);
}
A dirty trick you can use for cooldowns is having the coroutine switch a bool back to true after the time is up.
public bool countdown = true;
if(Input.GetKeyDown(KeyCode.Space) && countdown == true){
countdown = false;
StartCoroutine(Timer());
}
IEnumerator Timer(){
Debug.Log(“waiting 10 seconds”);
// waits 10 seconds (use what you learned previously to make this happen)
countdown = true;
}```
@untold drift you should be able to figure it out after reading this 
the first example gives you all you’ll need to know to fix your code to properly use a coroutine, and the second one gives a pretty big hint on how you can implement a cooldown
Btw, what does "yeild return" do? Because I know what "yeild return new WaitForSeconds("Seconds")" do. But if I dont type that i get the error: " 'ShootBullet()': not all code paths return a value." so I always type yeild return null in my coroutines.
yield is a special keyword for IEnumerables and IEnumerator: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/yield
but that doesn’t completely answer the question. Pretty much whenever you replace void with something, you have to return a value. So something like
public int DoubleEvenIntegers(int num)
{
if (num % 2 == 0) // checks if even
{
return num * 2;
}
else
{
// nothing
}
}```
will throw an error because the compiler is like *“what do i return if the number is odd???”*
So a way to fix that would be to just `return num;` in the else statement instead of doing nothing
That’s why the error says “all code paths must return a value”. It pretty much wants a value (whether null or whatever) for every possible circumstance( or code path )
If you don’t know what the return key word is, i’d suggest checking out this example that i sent to someone else before #archived-code-general message