- Home /
Help!Error CS0266
using UnityEngine; using System.Collections; using System.Collections.Generic;
public class Item : MonoBehaviour {
public string itemName;
public int itemID;
public string itemDescription;
public Texture2D itemIcon;
public int itemPower;
public int itemSpeed;
public int itemDamage;
public bool multiItems;
public int itemCounter;
private static int autoIndex = 0;
public ItemType itemType;
public enum ItemType{
Material,
Placable,
Hand,
Head,
Chest,
Legs,
Boots,
Food,
Water,
Weapon,
Tool,
Other,
}
public Item(){
}
public Item(ItemType type) {
itemType = type;
}
public Item(Dictionary<string, string> dict) {
itemName = dict["itemName"];
itemID ++;
itemDescription = dict["itemDescription"];
itemIcon = Resources.Load<Texture2D> ("ItemIcons/" + dict["itemName"]);
itemType = System.Enum.Parse(typeof(Item.ItemType), dict ["itemType"].ToString ());
itemPower = int.Parse (dict["itemPower"]);
itemSpeed = int.Parse (dict["itemSpeed"]);
itemDamage = int.Parse (dict["itemDamage"]);
multiItems = bool.Parse (dict["multiItems"]);
itemCounter = multiItems ? 1 : 0;
}
public Item (string name, string desc,int power,int speed,int damage,ItemType iType, bool mItems){
itemName = name;
itemID = autoIndex++;
itemDescription = desc;
itemIcon = Resources.Load<Texture2D>("ItemIcons/" + name);
itemSpeed = speed;
itemType = iType;
multiItems = mItems;
itemCounter = multiItems ? 1 : 0;
}
public Item (string name, int id,string desc,int power,int speed,int damage,ItemType type, bool mItems){
itemName = name;
itemID = id;
itemDescription = desc;
itemIcon = Resources.Load<Texture2D> ("ItemIcons/" + name);
itemSpeed = speed;
itemType = type;
multiItems = mItems;
itemCounter = multiItems ? 1 : 0;
}
public Item (string name, int id,string desc,int power,int speed,int damage,ItemType type, bool mItems,int counter){
itemName = name;
itemID = id;
itemDescription = desc;
itemIcon = Resources.Load<Texture2D> ("ItemIcons/" + name);
itemSpeed = speed;
itemType = type;
multiItems = mItems;
itemCounter = counter;
}
} is code I get error Cs0266 "Cannot implicitly convert type 'object' to Item.ItemType.An explicit conversation exists.(Are you using a cast?). Line 54,17.HELP!!!
Answer by Dimling · Oct 24, 2014 at 07:45 AM
The following code is the one that crashes your code:
itemType = System.Enum.Parse(typeof(Item.ItemType), dict ["itemType"].ToString ());
And the reason is as the error says: Cannot implicitly convert type 'object' to Item.ItemType.An explicit conversation exists.
So what you have to do is to append (ItemType) in front of the System.Enum part.
So simply just change the code above with the following code:
itemType = (ItemType)System.Enum.Parse(typeof(Item.ItemType), dict ["itemType"].ToString ());
Now it should work! :)
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Can you help with my errors? All are similar... 1 Answer
Runtime Error 1 Answer
Hack And Slash Tutorial - Character Generator [Errors] 0 Answers