- Home /
error CS0841: A local variable `holdSound' cannot be used before it is declared
Hi, I'm having trouble with this script. I am trying to use a shoot script, but it gives out the error that I havent declared the variable, but it also appears a warning that says that I declared it but it isn't used, how can I fix it? It seems like it was reading the script backwards. The error reads : "error CS0841: A local variable `holdSound' cannot be used before it is declared" Line 63
Gun Script:
using UnityEngine;
using System.Collections;
public class Shoot : MonoBehaviour {
public float holdHeight= -0.5f;
public float holdSide = 0.5f;
public float racioHipHold = 1f;
public float hipToAimSpeed = 0.1f;
private float racioHipHoldV ;
public float aimRacio = 0.4f;
public float zoomAngle = 30f;
public float fireSpeed = 15f;
private float waitTilNextFire = 0f;
public GameObject bullet;
public GameObject bulletSpawn;
public float shootAngleRandomizationAiming = 5f;
public float shootAngleRandomizationNotAiming = 15f;
public float recoilAmount = 0.5f;
public float recoilRecoverTime= 0.2f;
private float currentRecoilZPos;
private float currentRecoilZPosV;
public GameObject bulletSound;
public GameObject muzzleFlash;
public GameObject muzzleFlashSpawn;
//Unlocks
private bool weaponUnlocked = false;
void Update ()
{
if (Input.GetButton("Fire1"))
{
if (waitTilNextFire <= 0)
if (weaponUnlocked == true)
{
if (bullet)
{
Instantiate(bullet, bulletSpawn.transform.position, bulletSpawn.transform.rotation);
}
if (bulletSound)
{
var holdSound = Instantiate(bulletSound, bulletSpawn.transform.position, bulletSpawn.transform.rotation);
}
if (muzzleFlash)
{
var holdMuzzleFlash = Instantiate(muzzleFlash, muzzleFlashSpawn.transform.position, muzzleFlashSpawn.transform.rotation);
}
currentRecoilZPos -= recoilAmount;
waitTilNextFire = 1;
if (holdSound)
{
holdSound.transform.parent = transform;
}
if (holdMuzzleFlash)
{
holdMuzzleFlash.transform.parent = transform;
}
}
}
waitTilNextFire -= Time.deltaTime * fireSpeed;
currentRecoilZPos = Mathf.SmoothDamp(currentRecoilZPos, 0f, ref currentRecoilZPosV, recoilRecoverTime);
}
}
Answer by Landern · Jul 09, 2014 at 05:32 PM
on line: 53 you create the variable, once 54 is hit(the ending curly brace) the variable is out of scope and not available.
Do this and move it out of the if and where it can be accessed and resued:
void Update ()
{
if (Input.GetButton("Fire1"))
{
if (waitTilNextFire <= 0)
if (weaponUnlocked == true)
{
GameObject holdSound = null;
GameObject holdMuzzleFlash = null;
if (bullet)
{
Instantiate(bullet, bulletSpawn.transform.position, bulletSpawn.transform.rotation);
}
if (bulletSound)
{
holdSound = Instantiate(bulletSound, bulletSpawn.transform.position, bulletSpawn.transform.rotation);
}
if (muzzleFlash)
{
holdMuzzleFlash = Instantiate(muzzleFlash, muzzleFlashSpawn.transform.position, muzzleFlashSpawn.transform.rotation);
}
currentRecoilZPos -= recoilAmount;
waitTilNextFire = 1;
if (holdSound)
{
holdSound.transform.parent = transform;
}
if (holdMuzzleFlash)
{
holdMuzzleFlash.transform.parent = transform;
}
}
}
waitTilNextFire -= Time.deltaTime * fireSpeed;
currentRecoilZPos = Mathf.SmoothDamp(currentRecoilZPos, 0f, ref currentRecoilZPosV, recoilRecoverTime);
}
Edit: holdMuzzleFalsh would follow the same issues as holdSound, updated to resolve.
You're correct, the same issue will occur, i will update.