Hello everyone! I'm a relative beginner at C# and was hoping to get some help from the community regarding the error (CS0151) that this script is receiving. The aim of the script is to serialize the inputs into an XML document using the CreateCard() function at the bottom. The problem arises when I use a switch() function. The exact description of the error is "Assets/Scripts/CardManager.cs(91,17): error CS0151: A switch expression of type `System.Type[]' cannot be converted to an integral type, bool, char, string, enum or nullable type" It is possible that the error comes from cardType being an enum as the error states it can't be converted into an enum, but then how would I go about creating the categories for each XML section? Any help would be greatly appreciated! Code (CSharp): using UnityEngine; using System.Collections; using System; using System.Xml.Serialization; using System.IO; public class CardManager : MonoBehaviour { public enum CardTypes { EQUIPMENT, CHANT, SUMMON, SHAMAN, GEMSTONE } public enum Factions{ CHAOS, WATER, STRENGTH, NEUTRAL, AETHER, LIFE } public CardTypes cardType; public Factions faction; public string cardName; public string baseSprite; public enum ChantTypes{ BASIC, QUICK } public ChantTypes chantType; public enum EquipTypes1{ ARMOR, WEAPON, } public EquipTypes1 equipType1; public enum EquipTypes2{ HEAD, HANDS, FEET, TRINKET, RING, BACK, CHEST, LEGS, MAINHAND, OFFHAND, ONEHAND, TWOHAND } public EquipTypes2 equipType2; public enum SummonTypes1{ CREATURE, STRUCTURE } public SummonTypes1 summonType1; public enum SummonTypes2{ SOLDIER, BEAST, MORPHLING, DISCIPLE, ARCANE } public SummonTypes2 summonType2; public int health; public int armor; public int attack; public int manaCost; public int speed; public int cardID; public int gemstones; public int cardEffectID; public void CreateCard(){ CardContainer cardContainer = new CardContainer (); Type[] cardType = {typeof(BaseEquipment), typeof(BaseChant), typeof(BaseSummon)}; FileStream fs = new FileStream (Path.Combine (Application.streamingAssetsPath, "Cards.xml"), FileMode.Open); XmlSerializer serializer = new XmlSerializer (typeof(CardContainer), cardType); cardContainer = (CardContainer)serializer.Deserialize (fs); serializer.Serialize (fs, cardContainer); fs.Close (); switch (cardType){ //This line causes the CS0151 error case CardTypes.SUMMON: cardContainer.Summons.Add(new BaseSummon(cardName, manaCost, faction, speed, cardID, cardType, gemstones, cardEffectID, baseSprite, summonType1, summonType2, attack, health)); break; case CardTypes.CHANT: cardContainer.Chants.Add (new BaseChant(cardName, manaCost, faction, speed, cardID, cardType, gemstones, cardEffectID, baseSprite, chantType)); break; case CardTypes.EQUIPMENT: cardContainer.Equipment.Add (new BaseEquipment(cardName, manaCost, faction, speed, cardID, cardType, gemstones, cardEffectID, baseSprite, equipType1, equipType2, armor, attack)); break; case CardTypes.SHAMAN: break; case CardTypes.GEMSTONE: break; } fs = new FileStream (Path.Combine (Application.streamingAssetsPath, "Cards.xml"), FileMode.Create); serializer.Serialize (fs, cardContainer); fs.Close (); } }
You have a public cardType member, but you've also declared a local cardType variable that is an array of types. You need to either use switch (this.cardType) to make sure you reference the class member, or you need to rename the cardType array to something else. I would suggest the latter.
Thank you for the response! I actually did end up resolving the issue. It was a bit more complicated than renaming cardType -> other issues followed after doing so. But this was a great starting point to figuring out what was wrong! Thank you again!
fyi the compiler kicks out at the first error it runs into, so it doesn't tell you everything that is wrong. So solving the first problem can then let the compiler get further and run into the next issue if there is one.