Having an issue with using an Enum and using a string variable
Hello all, I'm trying to use a switch statement with an enum. what I using it for is i have a gameObject itemPickUp and in the script i have:
public string itemType; // string to store the item name.
public int inventoryAmountToAdd; // an int to store the amount to add to the inventory.
void Start () {
playerScript = GameObject.FindGameObjectWithTag("Player"); // Finds the target by the tag assigned to the game object.
}
void OnTriggerEnter(){
playerScript.GetComponent<Player>().PickUp(itemType, inventoryAmountToAdd);
Destroy(gameObject);
}
That part works, but in the Player.cs script in the PickUp() function:
public void PickUp(string item, int amount){
switch(item){
case itemPickUps.SMALLHEALTH:
break;
case itemPickUps.MEDIUMHEALTH:
break;
case itemPickUps.LARGEHEALTH.ToString():
break;
case itemPickUps.SMALLSTAMINA.ToString():
break;
case itemPickUps.MEDIUMSTAMINA.ToString():
break;
case itemPickUps.LARGESTAMINA.ToString():
break;
case itemPickUps.PISTOLAMMO.ToString():
break;
case itemPickUps.RIFLEAMMO.ToString():
break;
case itemPickUps.PISTOL.ToString():
break;
case itemPickUps.RIFLE.ToString():
break;
case itemPickUps.PLASMASWORD.ToString():
break;
default:
break;
}// end of switch(itemPickUps)
}
as you see in the first two cases I've tried just using the enum and then the name but it gives the error Cannot implicity convert type 'Player.itemPickUps' to string, so I've tried using the .ToString() method and get the following error A constant value us expected.
If anyone could help me with this it would be greatly appreciated.
why even use the string parameter in Pickup()
? use the enum
wherever possible - it'll cause fewer issues. if you do need to convert it to a string for display purposes, just handle that at the time.
also, where you get the reference to the player, why not also get the Player
component too? save doing it every time OnTriggerEnter()
calls (unless you have more than one player) Get Component isn't the fastest function in unity...