Referring GameObjet's script from array
Hi.
I have a script called scr_WeaponController which stores all player's weapons in an array. I use the following script to switch between the weapons:
void SwitchWeapon(int index)
{
for (int i = 0; i < weaponCount; i++)
{
if (i == index)
{
weapons[i].gameObject.SetActive(true);
}
else {
weapons[i].gameObject.SetActive(false);
}
}
}
Now each weapon object contains a script: "scr_weapon" with a function: "GetWeapon()" which sends all the information about said weapon (damage per shot, time between bullets, bullet spread etc.) forward to another script which does the shooting.
I'm trying to access the scr_Weapon script from the scr_WeaponController script like so:
void SwitchWeapon(int index)
{
for (int i = 0; i < weaponCount; i++)
{
if (i == index)
{
weapons[i].gameObject.SetActive(true);
weapons[i].GetComponent<scr_weapon>.GetWeapon();
}
else {
weapons[i].gameObject.SetActive(false);
}
}
}
Now it seems like I can't do it that way but I'm having a severe brain fart and can't figure out why. All help is greatly appreciated.
Thanks!
Answer by ComradeVanti · Mar 21, 2016 at 11:32 AM
Your script is almost correct. You just have a syntax error at line 9.
Here is the correctet line:
weapons[i].GetComponent<scr_weapon>().getWeapon();
You missed the "()" :)
Answer by tonsillectomy · Mar 21, 2016 at 11:42 AM
Oh you got to be kidding me!!
Thanks Brullix3000! I was getting quite desperate with that :D