Muzzle flash wont stay with gun when aiming.

I am making a FPS. I have a gun. I made a muzzle flash by adding a cube(naming it muzzle flash) and then flattening it out and adding a light(naming it muzzle light) in front of it. Adding the texture to the cube. Works really good. Flashes when i hit fire. So i added the cube and light as a child of the gun and positioned it to the front of the gun. This seems to be okay since it stays there following the player.

This is where my problem is. When i aim the gun it stays. It doesn’t follow the gun. I have an aim animation. How can i get the muzzle flash to follow the gun and not just stay in one place?

Keep in mind the muzzle flash is a child of the gun. The gun has a weapons camera and the gun is a child of weapons camera.

 public Renderer MuzzleFlash;  //cube
 public Light muzzleLight;     //light

IEnumerator fireBullet(){

		RaycastHit hit = new RaycastHit();
		
		enemy = (AIDamage)FindObjectOfType(typeof(AIDamage));
		
		//holdMuzzleFlash2.transform.parent = transform;
		if(Canfire == true){

		Vector3 fwd2 = spawn.TransformDirection(Vector3.forward);
		Debug.DrawRay(spawn.position, fwd2);
		if(Physics.Raycast(spawn.position, fwd2, out hit, range)){
				
		if(hit.collider.tag == "wood"){
		Instantiate(woodImpact, hit.point, Quaternion.FromToRotation(Vector3.forward, hit.normal));
             }
					
		if(hit.collider.tag == "Untagged"){
		Instantiate(untaggedImpact, hit.point, Quaternion.FromToRotation(Vector3.forward, hit.normal));
		}
				
		if(hit.collider.tag == "metal"){
		Instantiate(metalImpact, hit.point, Quaternion.FromToRotation(Vector3.forward, hit.normal));

		Instantiate(untaggedImpact, hit.point, Quaternion.FromToRotation(Vector3.forward, hit.normal));
		 }
				
		if(hit.collider.tag == "barrel"){
		Instantiate(metalImpact, hit.point, Quaternion.FromToRotation(Vector3.forward, hit.normal));
		Instantiate(untaggedImpact, hit.point, Quaternion.FromToRotation(Vector3.forward, hit.normal));
			            
		barrel = (explodingbarrel)FindObjectOfType(typeof(explodingbarrel));
		barrel.damage(damage);
		}
				
				
		if(hit.collider.tag == "Dirt"){
		Instantiate(mydirtimpact, hit.point, Quaternion.FromToRotation(Vector3.forward, hit.normal));
		}

		if(hit.collider.tag == "Concrete"){
		Instantiate(concreteImpact, hit.point, Quaternion.FromToRotation(Vector3.forward, hit.normal));
            }
			
		if(hit.collider.tag == "Enemy")
		{
		enemy = (AIDamage)FindObjectOfType(typeof(AIDamage));
		enemy.damage(damage);
		}

		if(hit.rigidbody)
		hit.rigidbody.AddForceAtPosition(force * fwd2, hit.point);
		
			
		Canfire = false;
			PlayAudioClip(shootSound, transform.position, 1.7f);
				MuzzleFlash.renderer.enabled = true;
				muzzleLight.enabled = true;
				
				//StartCoroutine("showMuzzleFlash");
			yield return new WaitForSeconds(0.03f); //(0.05f);
				MuzzleFlash.renderer.enabled = false;
				muzzleLight.enabled = false;

		Canfire = true;
		bulletsLeft--;
	     Gui ();
			

		}		
	}
	}

If i need to get the position of the gun with transform could you help me?
Im sorry about the formatting. Thank you.

Okay, so when you right click it aims down sights? I assume this animation was created in a program like blender by simply moving the gun mesh inside the modeling program.

One thing about doing that is: The “World origin” in the modeling program is there the model “is” in Unity (It’s transform.position) so those sorts of animations do not effect positions and thus parented information.

However; if your animation is done through bones that are imported with the model, that’s a different story.

For anyone who comes looking for the muzzleflash issue when player moves , I solved this issue with the following code

GameObject MuzzleFlash = Instantiate(MuzzleFlashPrefab, FirePoint.position, FirePoint.rotation) as GameObject;
        MuzzleFlash.transform.parent = FirePoint.transform;