Cannot implicitly convert type 'UnityEngine.Texture2D' to 'UnityEngine.Sprite'

Hello, i have an issue with changing image of button via script.
When i write buttonImage.image=newImage i get error mentioned in the title of this thread.

New image is a texture2D, it must be changed to sprite but i dont know how to do that. Please help me

Convert your texture asset to sprite.

2 Likes

Changed it to sprite , but still im getting that error

Can you show me the code where this happens? Maybe i can help you better than.

Are you sure? You set it to Sprite in the import settings of the texture.

1 Like

You would have to Create your sprite from the texture

you can use the static Sprite.Create Method for this

1 Like

In script
public Texture2D itemIcon;

later this itemIcon = Resources.Load(“Item Icons/”+name);

And in other script im using GUI.DrawTexture thats why i cant change itemIcon to sprite because id be getting error in DrawTexture function. I must change itemIcon to sprite somehow

so why not just change it than?

as BoredMormon stated you would have to change it in the import settings.

other than that i can’t see the problem with Resources.Load(“Item Icons/”+name);

Because if i change it to Resources.Load(“Item Icons/”+name); i will get tons of errors in code where i do GUI.DrawTexture

try use the public Texture2D ThisTexture; and drag the texture from your assets in the inspector instead.