i get the errors The name ‘AK74’ does not exist in the current context and The name ‘STG44’ does not exist in the current context in both Debug.Log lines
any solution?
private void Start()
{
weapon = GetComponent<Weapon>();
Weapon AK74 = new Weapon(2, 30, 200);
Weapon STG44 = new Weapon(1, 35, 200);
_currentAmmoInClip = clipSize;
STG44.Setmagsize(_currentAmmoInClip);
_ammoInReserve = reservedAmmoCapacity;
STG44.Setfullmag(_ammoInReserve);
_canShoot = true;
}
private void Update()
{
Debug.Log(AK74.Getmagsize());
Debug.Log(STG44.Getmagsize());
}
>Solution :
Those variables are defined in the scope of Start method, and they will be deleted as soon as this function will finish. You want to store them in the object itself, so you have to declare them outside of function, in the class itself, like so:
Weapon AK74, STG44;
private void Start()
{
weapon = GetComponent<Weapon>();
AK74 = new Weapon(2, 30, 200);
STG44 = new Weapon(1, 35, 200);
_currentAmmoInClip = clipSize;
STG44.Setmagsize(_currentAmmoInClip);
_ammoInReserve = reservedAmmoCapacity;
STG44.Setfullmag(_ammoInReserve);
_canShoot = true;
}
private void Update()
{
Debug.Log(AK74.Getmagsize());
Debug.Log(STG44.Getmagsize());
}