How can the 'next' GameObject in a dynamic list (not an array) be accessed?

Fellow Coders,

I may have oversimplified this problem but I am trying to create a scene in which the Player can Collide with a Weapon and said Weapon added to the Players inventory with the use of a List (not an array). The scene centres around a 3rd Person Controller. I have coded in C Sharp.

On Collision between player and Weapon, I am able to add as many objects as possible to a list. I know how to instantiate Element 0 in the list, but I can’t seem to work out how to access the following/next elements in order to allow for Weapon Switching and how to make Element 0 of the following elements.

I’ve looked up many solutions for this but all seem to focus on Weapon Switching with an array which is relatively much easier given that the number of weapons are finite. Is there a way to implement a Weapon Switching system with the use of a dynamic list rather than an array or is this just not possible?

As an aside, when trying to Instantiate the weapon (including Element 0), the weapon seems to instantiate at its original position rather than the position of the weaponHolder (an empty game object which is a child of the player) - just wondering if anyone had any suggestions?

Thanks in advance for your help - any pointers in the right direction would be gratefully appreciated :slight_smile:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq; 

public class PlayerShooting : MonoBehaviour {
public List <GameObject> guns = new List <GameObject> ();

void OnCollisionEnter (Collision weapon)
	{
		if (weapon.gameObject.tag == "Weapon") {
			guns.Add (weapon.gameObject); 
			weapon.gameObject.GetComponent<Collider> ().enabled = false;
			weapon.gameObject.GetComponent<Renderer> ().enabled = false;
			Debug.Log ("WeaponAcquired!");
		}
	}

void GunSelector ()
{
	selectedGun = guns [0];
	if (Input.GetKeyDown (KeyCode.B)) {
		selectedGun.transform.parent = weaponHolder; 
		selectedGun.SetActive (true);
		selectedGun.gameObject.GetComponent<Renderer> ().enabled = true;
		selectedGun.gameObject.GetComponent<Collider> ().enabled = true;
		Instantiate (selectedGun, weaponHolder.position, weaponHolder.rotation); 
		Debug.Log ("Weapon Changed");
	}
}

A list is basically just a re sizeable array.
What you want to do is keep an integer variable which indicates the weapon “slot” and to make sure this integer never goes below 0 or above the total elements in the list.