So i am working on a FPS , now i wanted to make the code a little clean, now i have a script for eachweapon type , and when i want to equip previosly i will seacrh for what type of weapon it is , using getcomponent , as I need to assign some elements.
Now i made a script that does just that for me , Called Weaponscript.cs , now wehn i equip i give the weapon script the variables , wich after will assign them to the gun script. Now the problem:
When i equip , it gives me an error of a missing argument , it doesnt tell me what .
Now in the explorer all the variables are assigned. Is it to slow?
Here is the code:
The controler part:
private void SwitchWeapons()
{
if(Input.GetKeyDown(KeyCode.Alpha2))
{
Destroy(curweapon);
GameObject weapon = Instantiate(Secundary);
WeaponScript script = weapon.GetComponentInChildren<WeaponScript>();
script.gunCamera = gunCamera;
script.currentWeaponText = currentWeaponText;
script.currentAmmoText = currentAmmoText;
script.totalAmmoText = totalAmmoText;
script.gunicon = CurWeapon;
weapon.transform.SetParent(Gunholder);
curweapon = weapon;
}
else if (Input.GetKeyDown(KeyCode.Alpha1))
{
Destroy(curweapon);
GameObject weapon = Instantiate(Primary);
WeaponScript script = weapon.GetComponentInChildren<WeaponScript>();
script.gunCamera = gunCamera;
script.currentWeaponText = currentWeaponText;
script.currentAmmoText = currentAmmoText;
script.totalAmmoText = totalAmmoText;
script.gunicon = CurWeapon;
weapon.transform.SetParent(Gunholder);
curweapon = weapon;
}
}
Here is where i give the variables to the Weaponscript
And then the weapon script assigns the variables to the specific gunscript:
private void Awake()
{
HandgunScriptLPFP semi = gameObject.GetComponent<HandgunScriptLPFP>();
if (semi != null)
{
semi.gunCamera = gunCamera;
semi.currentWeaponText = currentWeaponText;
semi.currentAmmoText = currentAmmoText;
semi.totalAmmoText = totalAmmoText;
semi.gunicon = gunicon;
return;
}
AutomaticGunScriptLPFP auto = gameObject.GetComponent<AutomaticGunScriptLPFP>();
if (auto != null)
{
auto.gunCamera = gunCamera;
auto.currentAmmoText = currentAmmoText;
auto.totalAmmoText = totalAmmoText;
auto.gunicon = gunicon;
auto.currentWeaponText = currentWeaponText;
return;
}
PumpShotgunScriptLPFP pump = gameObject.GetComponent<PumpShotgunScriptLPFP>();
if (pump != null)
{
pump.gunCamera = gunCamera;
pump.currentAmmoText = currentAmmoText;
pump.totalAmmoText = totalAmmoText;
pump.gunicon = gunicon;
pump.currentWeaponText = currentWeaponText;
return;
}
}
it’s checks for the weapon type and assignes the variables to the script
and now the gun script part (this part its the same to all)
private void Start () {
//Save the weapon name
storedWeaponName = weaponName;
//Get weapon name from string to text
currentWeaponText.text = weaponName;
//Set total ammo text from total ammo int
totalAmmoText.text = ammo.ToString();
//Set image
gunicon.sprite = gunimage;
//Setup pos
mainbone.localPosition = Vector3.zero;
mainbone.localRotation = Quaternion.Euler(0,0,0);
transform.localPosition = mainbonepos;
transform.localRotation = Quaternion.Euler(mainbonerot.x, mainbonerot.y, mainbonerot.z);
//Weapon sway
initialSwayPosition = transform.localPosition;
//Set the shoot sound to audio source
shootAudioSource.clip = SoundClips.shootSound;
}
i tried to make the weapon script assign on start , it didnt work if you where wondering
Here is a video demonstarting it: