ALright, I have a basic gun, just to test my abilities, but I want to make a wait thingy, so 2 secs later I have ammo. How would I make a wait thingy? (I have tried waitforseconds no mater what I do it errors) ALso please try to make it like one line short. This is in CShrap, and is under void update()
2 Answers
2It’s easy once you get past all the oddly named functions:
You need to keep a flag in your update: bool waitingToReload
if(waitingToReload != true){
StartCoroutine(fireGun( waitingTime ));
}
make your waitingTime 2 seconds or whatever. Then declare an IEnumerator function. check the tutorial, they are good to learn from.
IEnumerator fireGun(float waitTime){
waitingToReload = true;
//you can do other stuff here too
yield return new WaitForSeconds (waitTime);
//or you can do it here too
waitingToReload = false;
}
You can’t yield in Update(). You must use Invoke, or InvokeRepeating to invoke a function, or use a Coroutine.
Invoke Example:
using UnityEngine;
using System.Collections;
public class InvokeExample : MonoBehaviour
{
void Start()
{
Invoke ("Count", 3);
}
void Count()
{
print ("Count function invoked!");
}
}
InvokeRepeating Example:
using UnityEngine;
using System.Collections;
public class InvokeRepeatingExample : MonoBehaviour
{
int count = 0;
void Start()
{
InvokeRepeating("Count", 1, 1);
}
void Count()
{
count++;
print (count);
}
}
Coroutine Example:
using UnityEngine;
using System.Collections;
public class CoroutineExample : MonoBehaviour
{
int count = 0;
void Start()
{
StartCoroutine(Count ());
}
IEnumerator Count()
{
while(true)
{
count++;
print (count);
yield return new WaitForSeconds(1);
}
}
}
Say you want to fire by clicking left mouse, then wait 1 second before you may fire again, here’s how you’d do that using a Coroutine, my method of choice in this case.
using UnityEngine;
using System.Collections;
public class WaitToFireExample : MonoBehaviour
{
bool canFire = true;
void Update()
{
if(canFire && Input.GetMouseButtonDown(0))
{
canFire = false;
StartCoroutine (WaitToFire(1));
}
}
IEnumerator WaitToFire(float t)
{
print (canFire);
yield return new WaitForSeconds(t);
canFire = true;
print (canFire);
}
}
Why not just "t = Time.time" then "if (Time.time > t+2f)"? You could use Coroutines but that is likely overkill if you're just playing around.
– HuacanachaA simple check will do like Huacanacha said. By the way, you can't use coroutines on Update, check this: http://unitygems.com/coroutines/
– jovinoRight. But you can start a Coroutine from Update() and the coroutine itself will resume just after subsequent each Update(), which is basically equivalent to running in Update() . If you use "yield return new WaitForSeconds(2f)" the Coroutine code will resume execution 2 seconds after initializing it.
– Huacanacha@Hauc, okay... so what do you mean? Like in csharp I make a... public float time = 1.1F; then I make an if.... like if (1.2 > time + 2F) \\code Like that?
– applejuicesOh oksy so like..... I have 2 voids, one that plays sounds and maybe an animation, whilst I have another that fills bulelts up so like... invoke(void2, 4) void void2() { if(input.getkeydown("r")) bullets = 12; } like that?
– applejuices