Search Unity

Error CS0151 (Help Please)

Discussion in 'Scripting' started by MJacobsen186, Jan 5, 2016.

  1. MJacobsen186

    MJacobsen186

    Joined:
    Aug 28, 2015
    Posts:
    21
    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):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System;
    4. using System.Xml.Serialization;
    5. using System.IO;
    6.  
    7. public class CardManager : MonoBehaviour {
    8.  
    9.     public enum CardTypes {
    10.         EQUIPMENT,
    11.         CHANT,
    12.         SUMMON,
    13.         SHAMAN,
    14.         GEMSTONE
    15.     }
    16.     public enum Factions{
    17.         CHAOS,
    18.         WATER,
    19.         STRENGTH,
    20.         NEUTRAL,
    21.         AETHER,
    22.         LIFE
    23.     }
    24.     public CardTypes cardType;
    25.     public Factions faction;
    26.     public string cardName;
    27.     public string baseSprite;
    28.     public enum ChantTypes{
    29.         BASIC,
    30.         QUICK
    31.     }
    32.     public ChantTypes chantType;
    33.     public enum EquipTypes1{
    34.         ARMOR,
    35.         WEAPON,
    36.     }
    37.     public EquipTypes1 equipType1;
    38.     public enum EquipTypes2{
    39.         HEAD,
    40.         HANDS,
    41.         FEET,
    42.         TRINKET,
    43.         RING,
    44.         BACK,
    45.         CHEST,
    46.         LEGS,
    47.         MAINHAND,
    48.         OFFHAND,
    49.         ONEHAND,
    50.         TWOHAND
    51.     }
    52.     public EquipTypes2 equipType2;
    53.     public enum SummonTypes1{
    54.         CREATURE,
    55.         STRUCTURE
    56.     }
    57.     public SummonTypes1 summonType1;
    58.     public enum SummonTypes2{
    59.         SOLDIER,
    60.         BEAST,
    61.         MORPHLING,
    62.         DISCIPLE,
    63.         ARCANE
    64.     }
    65.     public SummonTypes2 summonType2;
    66.     public int health;
    67.     public int armor;
    68.     public int attack;
    69.     public int manaCost;
    70.     public int speed;
    71.     public int cardID;
    72.     public int gemstones;
    73.     public int cardEffectID;
    74.  
    75.     public void CreateCard(){
    76.  
    77.         CardContainer cardContainer = new CardContainer ();
    78.  
    79.         Type[] cardType = {typeof(BaseEquipment), typeof(BaseChant), typeof(BaseSummon)};
    80.  
    81.         FileStream fs = new FileStream (Path.Combine (Application.streamingAssetsPath, "Cards.xml"), FileMode.Open);
    82.  
    83.         XmlSerializer serializer = new XmlSerializer (typeof(CardContainer), cardType);
    84.  
    85.         cardContainer = (CardContainer)serializer.Deserialize (fs);
    86.  
    87.         serializer.Serialize (fs, cardContainer);
    88.  
    89.         fs.Close ();
    90.  
    91.         switch (cardType){    //This line causes the CS0151 error
    92.    
    93.         case CardTypes.SUMMON:
    94.             cardContainer.Summons.Add(new BaseSummon(cardName, manaCost, faction, speed, cardID, cardType, gemstones,
    95.                                                      cardEffectID, baseSprite, summonType1, summonType2, attack, health));
    96.             break;
    97.         case CardTypes.CHANT:
    98.             cardContainer.Chants.Add (new BaseChant(cardName, manaCost, faction, speed, cardID, cardType, gemstones,
    99.                                                     cardEffectID, baseSprite, chantType));
    100.             break;
    101.         case CardTypes.EQUIPMENT:
    102.             cardContainer.Equipment.Add (new BaseEquipment(cardName, manaCost, faction, speed, cardID, cardType, gemstones,
    103.                                                            cardEffectID, baseSprite, equipType1, equipType2, armor, attack));
    104.             break;
    105.         case CardTypes.SHAMAN:
    106.             break;
    107.         case CardTypes.GEMSTONE:
    108.             break;
    109.  
    110.         }
    111.  
    112.         fs = new FileStream (Path.Combine (Application.streamingAssetsPath, "Cards.xml"), FileMode.Create);
    113.         serializer.Serialize (fs, cardContainer);
    114.         fs.Close ();
    115.     }
    116. }
    117.  
     
    Last edited: Jan 6, 2016
  2. Dave-Carlile

    Dave-Carlile

    Joined:
    Sep 16, 2012
    Posts:
    967
    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.
     
    MJacobsen186 likes this.
  3. MJacobsen186

    MJacobsen186

    Joined:
    Aug 28, 2015
    Posts:
    21
    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!
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    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.