Hi, i made a WeaponSwitch and want to reset the position of the weapon when i switch to another. At the moment i got a crossbow and a sword. When i zoom with the crossbow, switch to the sword und switch back, the crossbow stays zoomed.
I know i can handle it with a bool at the crossbow script but i want it dynamically for all weapons in the future. So i have an array with all weapons kept in. I create a second array where all weaponlocations are stored in.
But this array seems to be null, even when i declare it.
My code:
public GameObject[] weapons;
Vector3[] weaponStartLocation;
int currentWeapon = 0;
int weaponNumber;
void Start()
{
weaponNumber = weapons.Length;
// Start with Sword
SwitchWeapons(currentWeapon);
for (int i = 0; i < weapons.Length; i++)
{
weaponStartLocation[i] = weapons[i].transform.localPosition;
}
}
void Update ()
{
for (int i = 1; i <= weaponNumber; i++)
{
if (Input.GetKeyDown("" + i))
{
currentWeapon = i - 1;
SwitchWeapons(currentWeapon);
}
}
}
void SwitchWeapons(int index)
{
for (int i = 0; i < weaponNumber; i++)
{
if (i == index)
{
weapons[i].gameObject.SetActive(true);
weapons[i].transform.localPosition = weaponStartLocation[i];
}
else
{
weapons[i].gameObject.SetActive(false);
}
}
}
On the Startmethod, i store all Locations. At the WeaponSwitch i use this array to handle the weapon positions.
Any help =?