Question by
Platiniumekk · Nov 29, 2016 at 09:25 PM ·
getcomponentcommunicationmodifymodification
How to modify variables from second script.
Hi! I want make reloading weapon and i need to modify variables from weapon script. I dont know how to do this because GetComponent does not allow this. Script in weapon:
public class StatystykiBroni : MonoBehaviour
{
//-----------------------------------------Ammo \/
public int magazynki = 6;
public int ammoInMags = 30;
public int ammoInGun = 0;
public int komora = 0;
}
Void in Script from FpsController:
void Ammo()
{
StatystykiBroni wpn = gameObject.GetComponentInChildren<StatystykiBroni>();
if (Input.GetButtonDown("Reload") && (wpn.ammoInGun < wpn.ammoInMags) && wpn.magazynki > 0)
{
if (wpn.ammoInGun != 0)
{
wpn.komora = 1;
}
wpn.magazynki--;
wpn.ammoInGun = wpn.ammoInMags + wpn.komora;
wpn.komora = 0;
}
}
}
Thanks for answers!
Comment
Does it work if you remove the gameObject from the GetComponentInChildren?
$$anonymous$$y bad. In function responsible for the shot i hadn't ammoInGun--; But thanks for commitment