Shooting and Flash

I have a shooting script where you press to shoot and then it calls a function. Then it should instantiate a flash object, and disappear after 0.2 seconds. The problem is the shoot function stops calling immediately after its called, making it not possible to destroy.

using UnityEngine;

using System.Collections;

public class Shooting : MonoBehaviour

{

public float fireRate = 0.25f;
private float nextFire;
public GameObject spark;
public GameObject flash;
public Transform gunTip;
public LayerMask layer;
public float range = 10f;
public float flashYield = 0.2f;
private float nextFlash;

void Start () 
{
	nextFire = fireRate;
	nextFlash = flashYield;
}

void Update () 
{
	if(fireRate < nextFire)
	{
		fireRate += Time.deltaTime;
	}

	if(Input.GetButton("Fire1") && fireRate >= nextFire)
	{
		Shoot();
		fireRate = 0f;
	}
}

void Shoot()
{
	Vector2 mousePos = new Vector2(Camera.main.ScreenToWorldPoint(Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
	Vector2 firePoint = new Vector2(gunTip.position.x, gunTip.position.y);

	RaycastHit2D hit = Physics2D.Raycast(firePoint, mousePos - firePoint, range, layer);

	FlashF();
	SparkF();
}

void FlashF()
{
	GameObject flashClone = Instantiate(flash, gunTip.position, gunTip.rotation) as GameObject;
	if(flashYield >= 0)
	{
		flashYield -= Time.deltaTime;
	}
	else
	{
		flashYield = nextFlash;
		Destroy(flashClone);
	}
}

void SparkF()
{

}

Instead of creating and destroying the flash you could attach it as a sub-object to the gun. Then you just need to show/hide it with renderer.enabled = true/false using a timer.

You are only calling FlashF as many times as you are calling Shoot. You are calling shoot once every 0.25 seconds the user has the fire button held down. Therefore there is no reason for you to keep track of time in FlashF.

I think what you want to do instead is add a different script to your flashClones who keeps track of time on it’s own. That way each flash knows when it’s supposed to die and all your Shooting class has to do is instantiate flashclones.