Reset Weapon Position on Weapon Switch

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 =?

Are you dragging every weapon you have into the script’s weapons array in the editor? if not thats why its null.

Of course i do :slight_smile:

Sorry I just reread your OP. Its weaponStartLocation that is null right?
you need to do this since its not a public variable with its value set in the editor:

 Vector3[] weaponStartLocation = new Vector3[weapons.Length];

Your code just tells the compiler “Hey i’m gonna have a variable called weaponStartLocation and its going to be a List of Vectors. I’m not sure how big yet… I’ll let you know later.”

This code actually tell the compiler how much room to set aside for it.

You didn’t have to do this for weapons, because Unity did that for you behind the scenes when you dragged objects into it.

Yeah i just saw this error by myself, but thanks a lot. The prob is another:

I just want to set the bool “isZoomed” of the crossbow to false, when i switch a weapon.

Thats the whole thing but i dont know how to do :S

Do these weapon all have the same script attached to them? if so you can do this:

   weapons[i].gameObject.SetActive(true);
   weapons[i].transform.localPosition = weaponStartLocation[i];
   WeaponScript script = weapons[i].GetComponent<WeaponScript>();
    script.zoom = false;

Yes I would use that looks alright and works for me!