changing weapons using an array/list

I’m using the mouse wheel to change between weapons in a list. The player starts off with only one weapon, and more are added when he collides with them in game.

I’m struggling with adding items to a list in a different script, (both scripts are on the player).

There are prefabs called “weapon,” a few of which are placed in my scene. The prefab has text above it, which I’m using to change the name to pistol or shotgun or whatever. When the player triggers a collision, the prefab is deleted and I want to add another item to the inventory attached to my player. The items to add are simply empty game objects with a name like sword or pistol etc. They are not placed in the scene.

The inventory displays all the weapons the player has picked up as GUI Labels and using the mouse wheel allows him to scroll the list, selecting different weapons. Getting Unity to display which weapon is currently selected on screen is a problem for another time.

Here is some of the code I’m using:

On my player object, I have the following in my update function:

var invList : weapswitch2;
invList = gameObject.GetComponent(weapswitch2); //weapswitch2 is the name 
												//of the script I'm trying access.

The collisions with the weapon pickups are done like this:

function OnTriggerEnter(other:Collider)
{ 

	if(other.gameObject.name == "weapon")
	{
		//add to array
		invList.weapons.Add(sword.GameObject); //weapons is the name of the list                          
		
		Destroy (other.gameObject);
	}
}

The list is displayed on screen like this:

function OnGUI()
{
	var yOffset : float = 0.0;
    for (var i : int = 0; i <= weapons.Count-1; i++)
    {
      GUI.Label (Rect (10, 10 + yOffset, 100, 20), weapons*.name);*

if (weapons*){*
//code for each on screen item goes here
}
yOffset += 35;
}
}
To clarify, when the player triggers a collision with a “weapon” prefab, I need to check the name of the text object attached to the prefab, (e.g. pistol) and add a corresponding item to the list, which is then displayed on screen.
This gives 2 errors:
“unknown identifier invList” & “unknown identifier sword.”
If I can sort those out and work out how to add/remove objects from the list, I can move on to adding objects based on name etc.
I’ve searched pretty extensively for a solution and whilst there are posts and documents which are related, I’m still running into the errors.
Thanks

invList = gameObject.GetComponent(weapswitch2); //weapswitch2 is the name

Firstly, you need to be using the right type - GameObject instead of gameObject (Case matters). Also, if the script is not attached to the same GameObject, you will need to find the GameObject first. I’ll provide an example of Find, but you really should use FindWithTag or something more optimised as using Find is not a good practise. You should also cast the type in the GetComponent command (See after = )

var GameObNeeded : GameObject;
GameObNeeded = GameObject.Find("NameOfGameObject");

invList = (weapswitch2)GameObNeeded.GetComponent("weapswitch2"); //weapswitch2 is the name

also

function OnTriggerEnter(other:Collider)
{

if(other.gameObject.name == "weapon")
{
   //add to array
   invList.weapons.Add(sword.GameObject); //weapons is the name of the list                          

   Destroy (other.gameObject);
}

}

My concern here is the following:

invList.weapons.Add(sword.GameObject); //weapons is the name of the list   

Where is sword declared?