Grabbing an enum object with an index (int)

Hello!

I want to set up a list to contain “containers” for all different ammo types in my game for my player character gameobject, but I’ve hit a dead end while trying to do so.

I’ve made a for loop to create all types of ammo, but then when I want to assign the correct ammo type to each of those ammo objects I don’t know how to grab it from the enum with the int I use as index. I’ld appreciate some guidance on this :))
(I noticed now I haven’t added the object to my list either but that was just me being sloppy, neways that’s not the problem:))

	private void SetupAmmoStock() {

	for( int cnt = 0; cnt < System.Enum.GetValues( (int)AmmoType.End ); cnt++ ) {

		ammoStocks[cnt] = new Ammo();

		

		ammoStocks[cnt].TypeOfAmmo = GetAmmoType( cnt );

		ammoStocks[cnt].CalculateMaxAmmo();

	}

}



private AmmoType GetAmmoType( int index ) {

	//I want to return the correct ammo type here, but obviously my line of code doesn't work

	//return System.Enum.GetValues(( (int)AmmoType)index );

}

1 Answer

1

Just cast it.

AmmoType theEnum = AmmoType.End;
int toInteger = (int)theEnum;
AmmoType andBackAgain = (AmmoType)toInteger;

Worked for me, too! Same problem here. Sorry I can't upvote.