Adrianna Tyszkiewicz
Adrianna Tyszkiewicz

Reputation: 63

Create new Sprite() C# Unity

I'm trying to create a function that matches the icon based on the item type. When I find the item type, I want to assign the appropriate image to the Sprite object and return it.

However, I keep getting the error "Element Sprite does not contain a constructor accepting the following number of arguments".

private Sprite ReturnItemIcon(BaseItem item)
    {
        Sprite icon = new Sprite();

        if(item.ItemType == BaseItem.ItemTypes.ELIXIR)
        {
            BaseElixirItem elixiritem = (BaseElixirItem)item;

            if(elixiritem.ElixirType == BaseElixirItem.ElixirTypes.ORDINARY)
            {
                icon = Resources.Load<Sprite>("ItemIcons/ordinaryelixir");
            } 
            else if (elixiritem.ElixirType == BaseElixirItem.ElixirTypes.RARE)
            {
                icon = Resources.Load<Sprite>("ItemIcons/rareelixir");
            } 
            else if (elixiritem.ElixirType == BaseElixirItem.ElixirTypes.LEGENDARY) 
            {
                icon = Resources.Load<Sprite>("ItemIcons/legendaryelixir");
            }
        } 
        else if(item.ItemType == BaseItem.ItemTypes.EQUIPMENT)
        {
            //sprawdzenie typow przedmiotow
        } 
        else if(item.ItemType == BaseItem.ItemTypes.CHEST)
        {
            //i tu tez
        }

        return icon;
    }

I will add that I follow the tutorial: TUTORIAL

This problem does not occur in the tutorial. I guess it's because of the older version of Unity?

How can I solve this problem so that I don't have to change my concept?

Upvotes: 0

Views: 1498

Answers (2)

derHugo
derHugo

Reputation: 90639

In general you don't use new you use Sprite.Create like e.g.

sprite = Sprite.Create(tex, new Rect(0.0f, 0.0f, tex.width, tex.height), new Vector2(0.5f, 0.5f), 100.0f);

In your case however all you need is to initialize the variable with icon = null. No need to create a sprite you would throw away anyway.

private Sprite ReturnItemIcon(BaseItem item)
{
    Sprite icon = null;

    // then you should better use switch-case blocks
    switch(item.ItemType)
    {
        case BaseItem.ItemTypes.ELIXIR:
            BaseElixirItem elixiritem = (BaseElixirItem)item;

            switch(elixiritem.ElixirType)
            {
                case BaseElixirItem.ElixirTypes.ORDINARY:
                    icon = Resources.Load<Sprite>("ItemIcons/ordinaryelixir");
                    break;

                case BaseElixirItem.ElixirTypes.RARE:
                    icon = Resources.Load<Sprite>("ItemIcons/rareelixir");
                    break;

                case BaseElixirItem.ElixirTypes.LEGENDARY:
                    icon = Resources.Load<Sprite>("ItemIcons/legendaryelixir");
                    break;

                default:
                    throw new ArgumentOutOfRangeException(nameof(elixiritem.ElixirType), elixiritem.ElixirType, null);
            }
            break;

        case BaseItem.ItemTypes.EQUIPMENT:
            //sprawdzenie typow przedmiotow
            break;

        case BaseItem.ItemTypes.CHEST:
            //i tu tez
            break;

        default:
            throw new ArgumentOutOfRangeException(nameof(item.ItemType), item.ItemType, null);
    }

    return icon;
}

Or alternatively simply do not use a "global" variable but declare them as close to usage as possible:

private Sprite ReturnItemIcon(BaseItem item)
{
    switch(item.ItemType)
    {
        case BaseItem.ItemTypes.ELIXIR:
            BaseElixirItem elixiritem = (BaseElixirItem)item;

            switch(elixiritem.ElixirType)
            {
                case BaseElixirItem.ElixirTypes.ORDINARY:
                    return Resources.Load<Sprite>("ItemIcons/ordinaryelixir");

                case BaseElixirItem.ElixirTypes.RARE:
                    return Resources.Load<Sprite>("ItemIcons/rareelixir");

                case BaseElixirItem.ElixirTypes.LEGENDARY:
                    return Resources.Load<Sprite>("ItemIcons/legendaryelixir");

                default:
                    throw new ArgumentOutOfRangeException(nameof(elixiritem.ElixirType), elixiritem.ElixirType, null);
            }

        case BaseItem.ItemTypes.EQUIPMENT:
            //sprawdzenie typow przedmiotow
            break;

        case BaseItem.ItemTypes.CHEST:
            //i tu tez
            break;

        default:
            throw new ArgumentOutOfRangeException(nameof(item.ItemType), item.ItemType, null);
    }
}

Just make sure every possible branch returns something (or ends in a throw anyway).

Upvotes: 3

Antoine Thiry
Antoine Thiry

Reputation: 2442

This should just work :

private Sprite ReturnItemIcon(BaseItem item)
{
    Sprite icon = null;

    if(item.ItemType == BaseItem.ItemTypes.ELIXIR)
    {
        BaseElixirItem elixiritem = (BaseElixirItem)item;

        if(elixiritem.ElixirType == BaseElixirItem.ElixirTypes.ORDINARY)
        {
            icon = Resources.Load<Sprite>("ItemIcons/ordinaryelixir");
        } 
        else if (elixiritem.ElixirType == BaseElixirItem.ElixirTypes.RARE)
        {
            icon = Resources.Load<Sprite>("ItemIcons/rareelixir");
        } 
        else if (elixiritem.ElixirType == BaseElixirItem.ElixirTypes.LEGENDARY) 
        {
            icon = Resources.Load<Sprite>("ItemIcons/legendaryelixir");
        }
    } 
    else if(item.ItemType == BaseItem.ItemTypes.EQUIPMENT)
    {
        //sprawdzenie typow przedmiotow
    } 
    else if(item.ItemType == BaseItem.ItemTypes.CHEST)
    {
        //i tu tez
    }

    return icon;
}

Another option would be to initialize the sprite with a white rectangle with the help of Sprite.Create

Upvotes: 0

Related Questions