Luzan Baral
Luzan Baral

Reputation: 3698

Passing Sprite as arguments to Constructor in Unity

I have a class Item as below

[System.Serializable]
public class Item
{
    public string itemName;
    public Sprite icon;
    public float price = 1f;

    public Item(string newItemName, Sprite newIcon, float newPrice)
    {
        this.itemName = newItemName;
        this.icon = newIcon;
        this.price = newPrice;
    }
}

Then, populating the List of Item during Start on different class and here's the code.

void Start () {
    itemList = new List<Item>();
    TextAsset asset = Resources.Load(Path.Combine("Maps", "items")) as TextAsset;
    var items = JsonHelper.FromJson<Item>(asset.text);
    Debug.Log("" + items.Length);
    for(int i= 0; i < items.Length; i++)
    {
        itemList.Add(new Item(items[i].itemName, Resources.Load<Sprite>(items[i].icon), items[i].price));
    }
    RefreshDisplay();
}

And here's my items.txt file inside Assets/Resources/Maps/

{"Items":[{
    "id": "1",
    "itemName": "Candy",
    "price": "2",
    "icon" : "ic_candy"
  },
  {
    "id": "2",
    "itemName": "Coin",
    "price": "10",
    "icon" : "ic_coin"
  },
  {
    "id": "3",
    "itemName": "Bomb",
    "price": "8",
    "icon" : "ic_bomb"
  }]
}

I get error saying

Argument 1: Cannot convert from 'UnityEngine.Sprite' to 'string'

at itemList.Add(new Item(items[i].itemName, Resources.Load<Sprite>(items[i].icon), items[i].price));

I tried sending pathname (items[i].icon) of sprite to the constructor but still got the error.

What can be the fix?

Upvotes: 1

Views: 1125

Answers (1)

Programmer
Programmer

Reputation: 125275

Here is the problem:

Resources.Load<Sprite>(items[i].icon)

The icon variable from the Item class is Sprite type but Resources.Load is a function that takes path which is a string type in its parameter. To cut this short, you are passing Sprite to a parameter that expects string.

The simple solution is to add another variable called public string iconPath;. You then use it to load the Sprite to your public Sprite icon; variable.

Something like below should do it:

[System.Serializable]
public class Item
{
    public string itemName;
    public string iconPath;
    public float price = 1f;

    [System.NonSerialized]
    public Sprite icon;

    public Item(string newItemName, Sprite newIcon, float newPrice)
    {
        this.itemName = newItemName;
        this.icon = newIcon;
        this.price = newPrice;
    }
}

// Use this for initialization
void Start()
{
    itemList = new List<Item>();
    TextAsset asset = Resources.Load(Path.Combine("Maps", "items")) as TextAsset;
    var items = JsonHelper.FromJson<Item>(asset.text);
    Debug.Log("" + items.Length);
    for (int i = 0; i < items.Length; i++)
    {
        itemList.Add(new Item(items[i].itemName, Resources.Load<Sprite>(items[i].iconPath), items[i].price));
    }
}

Make sure to update your Json file to accommodate the new variable name.

Upvotes: 3

Related Questions