How can I use an "Enum e" I passed from another script in an If or Switch Statement
Good evening. I am trying to make a set of buttons that disable/enable an object in my card game; Every Card has an enum type called rarity, and an Instance of it called currentCR that I manually set to BRONZE For test purposes. So I created the following Function:
public void DisableVaultCard(GameObject vault, Enum e)
{
for(int i =0; i < vault.transform.childCount; i++)
{
//CardProperties is a script in every Card.
if(e == vault.transform.GetChild(i).GetComponent<CardProperties>().currentCR)
{
}
}
}
and It is being called from this Script:
public class DisableEnableCard : MonoBehaviour, IPointerClickHandler
{
public Image image;
public Sprite[] buttonState;
bool pressed = false;
private GameObject vault;
private CardGUILogic cardGUILogic = new CardGUILogic();
void Awake()
{
image = GetComponent<Image>();
vault = GameObject.Find("/DeckBuilderHolder/Card Picker Panel/Scroll View/Viewport/Content");
}
public void OnPointerClick(PointerEventData eventData)
{
pressed = !pressed;
if(pressed)
{
image.sprite = buttonState[1];
**cardGUILogic.DisableVaultCard(vault, CardProperties.rarity.BRONZE);**
}
else
{
image.sprite = buttonState[0];
**cardGUILogic.EnableVaultCard(vault, CardProperties.rarity.BRONZE);**
}
}
}
It would be much apreciated if someone told me how to make the if statement work. The Debug.Log(e) Displays the value BRONZE so I do not know what I am Missing.
There is nothing wrong with the code as far as I can see, Put a debug inside the if statement:
Debug.Log("E: " + e + " CurrentCR: " + currentCR);
And see if they are the same.
It had to do with my hierarchy as I Had each card inside a panel for Disable/Enable purposes without disabling the cards scripts.
Answer by Quetz00 · Feb 12, 2018 at 07:44 AM
I solved it already. I had to use e.Equals(vault.transform.GetChild(i).GetComponentInChildren().currentCR)
and it solved it. Thanks to everyone anyway. And also hope that this helps someone else.
Your answer
![](https://koobas.hobune.stream/wayback/20220612152444im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Is this bug known? function parameter s and if statements c# 1 Answer
Null Reference with ifstatements accessing enum types. 2 Answers
Enumaration error CS1025: Single-line comment or end-of-line expected 1 Answer
How to check to see if Switch Case is true. 1 Answer
How do I run an animation then destroy a game object? 0 Answers