- Home /
Delay when reloading
i wrote reload script so when i Click "R" i want it to reload in 3 second because my animation lasts 3 seconds. I tried Yield with IEnumerator but it didnt workedt. So there is the script:
using UnityEngine;
using System.Collections;
public class Shooting : MonoBehaviour {
public int inventoryAmmoCount = 30;
private int RaznicaAmmo;
private int CurAmmoCount = 10;
public int MaxAmmoCount = 10;
void Update () {
RaznicaAmmo = MaxAmmoCount-CurAmmoCount;
if(Input.GetButtonDown("Reload Weapon")&inventoryAmmoCount>0) //გადატენვის ღილაკი
{
float l = Time.time;
reloadi = l;
if(inventoryAmmoCount>=RaznicaAmmo)
{
CurAmmoCount += RaznicaAmmo; //გადატენვისას რო დაემატოს ზუსტად იმდენი ტყვია რამდენიც უკლია რომ შეივსოს
inventoryAmmoCount -= RaznicaAmmo; // აკლებს ტყვიების რაოდენობას საერთო რაოდენობას
}
else
{
CurAmmoCount+=inventoryAmmoCount;
inventoryAmmoCount = 0;
}
audio.PlayOneShot(Reload);
}
}
}
Sometimes when a Yield is not working I like to create my own timer by simply:
int timer = 100; //or your delay total
timer -= 1;
if (timer < 1)
{
//Run the code
timer = 100;
}
:) Try that maybe?
I would add the ammo when the animation is completed. http://forum.unity3d.com/threads/47807-Trigger-an-event-on-animation-complete http://docs.unity3d.com/Documentation/ScriptReference/Animation-isPlaying.html
@lasha_an what slayer29179 suggested above should work even if you are not holding R down but simply by pressing it. just try to play with your code some more.
Your answer
Follow this Question
Related Questions
How to translate code with yield waitforseconds to c#? 1 Answer
WaitForSeconds Question 2 Answers
Delay in a IEnumerator 2 Answers
Delay in Instantiate only runs once 2 Answers
Using yield wait for seconds in my script, correctly 2 Answers