I am creating a gun script and the yield WaitForSeconds(fireRate) is not starting up. See if you can spot a problem with it.
using UnityEngine;
using System.Collections;
public class Gun : MonoBehaviour {
#region Variables and Script Settings
public string gunName; //The name of the Gun
public GameObject projectilePrefab; //The prefab of the bullet
private Transform _myTransform; //The location of the weapon
public Texture2D HUD_Icon;
public float projectileSpeed = 500;
public float fireRate = .1f;
private bool _allowFire;
private Component _projectileRigidbody;
public bool debug = false;
#endregion
void Start() {
_myTransform = transform;
_projectileRigidbody = projectilePrefab.GetComponent<Rigidbody>();
if(_projectileRigidbody = null)
projectilePrefab.AddComponent<Rigidbody>();
_allowFire = true;
}
// Update is called once per frame
void Update () {
if(Input.GetButton("Trigger1")) {
if(_allowFire) {
LaunchProjectile();
}
}
}
void OnGUI() {
DisplayHUDWeaponTexture();
}
public void LaunchProjectile() {
_allowFire = false;
Vector3 startPosition;
startPosition = _myTransform.FindChild("Barrel").transform.position;
GameObject projectile = GameObject.Instantiate(projectilePrefab, startPosition, Quaternion.identity) as GameObject;
projectile.rigidbody.useGravity = false;
projectile.rigidbody.AddForce(projectile.transform.forward * projectileSpeed);
FireRateLimiter();
}
public IEnumerator FireRateLimiter() {
yield return new WaitForSeconds(fireRate);
_allowFire = true;
}
public void DisplayHUDWeaponTexture() {
GUI.Label(new Rect(20, 20, 100, 40), HUD_Icon);
}
}