Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

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

Discussion in 'Scripting' started by martis941, Dec 2, 2016.

  1. martis941

    martis941

    Joined:
    Feb 22, 2016
    Posts:
    95
    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
     
  2. Ironmax

    Ironmax

    Joined:
    May 12, 2015
    Posts:
    890
    Convert your texture asset to sprite.
     
    Xepherys and Kiwasi like this.
  3. martis941

    martis941

    Joined:
    Feb 22, 2016
    Posts:
    95
    Changed it to sprite , but still im getting that error
     
  4. Ironmax

    Ironmax

    Joined:
    May 12, 2015
    Posts:
    890
    Can you show me the code where this happens? Maybe i can help you better than.
     
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Are you sure? You set it to Sprite in the import settings of the texture.
     
    Ironmax likes this.
  6. vothka

    vothka

    Joined:
    Mar 27, 2015
    Posts:
    59
    You would have to Create your sprite from the texture

    you can use the static Sprite.Create Method for this
     
    Kiwasi likes this.
  7. martis941

    martis941

    Joined:
    Feb 22, 2016
    Posts:
    95
    In script
    public Texture2D itemIcon;

    later this itemIcon = Resources.Load<Texture2D>("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
     
  8. vothka

    vothka

    Joined:
    Mar 27, 2015
    Posts:
    59
    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<Sprite>("Item Icons/"+name);
     
  9. martis941

    martis941

    Joined:
    Feb 22, 2016
    Posts:
    95
    Because if i change it to Resources.Load<Sprite>("Item Icons/"+name); i will get tons of errors in code where i do GUI.DrawTexture
     
  10. Ironmax

    Ironmax

    Joined:
    May 12, 2015
    Posts:
    890
    try use the public Texture2D ThisTexture; and drag the texture from your assets in the inspector instead.