Clearing Dynamically GameObject Array

Hi,

well I created an array for my weapons, and when I change the selected slot of my Inventarory, I want to clear the last gameobject charged by the following code:

bool slotChanged;
int selectedSlot; //That changes
float tempselectedSlot;
int tempSelected;
GameObject Manager; //A simple GameObject that contains a Store
GameObject wCamera; //Another Script that contains PlayerWeapons
PlayerWeapons pWeapons;

void Start() {
    pWeapons = wCamera.GetComponent<PlayerWeapons>();
}

void Update() {

if (selectedSlot > 9) { //That supposedly avoids the array out of index error, but that doesn't avoid anything... Because sometimes that fails...
	tempselectedSlot = 0;		
} else if(selectedSlot < 0) {
	tempselectedSlot = 9;				
}

tempselectedSlot -= Input.GetAxis("Mouse ScrollWheel")/2; //Mouse ScrollWheel is equals to 1.
					
selectedSlot = (int)tempselectedSlot;

slotChanged = selectedSlot != tempSelected;
						
tempSelected = selectedSlot;

	if(slotChanged) { //If the selected Slot changes, that actives, and it supposedly clears the main weapon GameObject array, and if there is a new item in the selected slot, charges it...

		pWeapons.ClearArray(); //A JS Function that supposedly clears the main array.

		if(Slots.HotBarSlots[selectedSlot] != null) { //If the selected Slot contains a Element...
           		Manager.transform.Find("StoreController").gameObject.GetComponent<DBStoreController>().equipWeapon(pAvoider.transform.Find(Slots.HotBarSlots[selectedSlot].itemname).gameObject.GetComponent<WeaponInfo>(), 0); //That Calls the function equipWeapon, from Manager -> StoreController -> DBStoreController (JS Script)
        }
							
    }

}

equipWeapon works so I won’t put the code…

But ClearArray doesn’t works, so there is the problem:

//The problem is that it's in JS (because it's a very big third party code)

function ClearArray() {
var Dweapons = new Array(weapons); //Create a dinamically array from a builtin array
Dweapons.Clear(); //Clear it entire
Dweapons.length = 1; //Set the default length
weapons = Dweapons; //And return a builtin array...
}

//But for some reason it desn't work, the GameObject is still there when there isn't a item on the selected slot...

All is in the code comments, so read it, and tell me what I’m doing wrong…

Thanks in advance…
Bye.

You’re doing too much work. Replace

pWeapons.ClearArray();

with

pWeapons = [];

This will just set it to a brand new empty array, and you don’t need your function at all.