Home > Blockchain >  Cannot create Sprite with Resources.Load<> in Unity
Cannot create Sprite with Resources.Load<> in Unity

Time:11-10

I am trying to change my source image of UI.Image with script. You can see below that there are two versions with the former (the one commented) working but the latter not. I have tried to change the texture type from Default to Sprite (2D and UI) but it still not working. Can someone explain why?

//var tex = Resources.Load<Texture2D>("candy_110/candy1_green_01");
//GetComponent<Image>().sprite = Sprite.Create(tex, new Rect(0.0f, 0.0f, tex.width, tex.height), new Vector2(0.5f, 0.5f));
GetComponent<Image>().sprite = Resources.Load<Sprite>("candy_110/candy1_green_01");

Update 1: Because everyone wants to make sure that Resources.Load does not return any error, I post here 2 screenshots of the asset layout and the console. Screenshot of assets layout

Screenshot of console log

CodePudding user response:

You can try changing the Sprite Mode import setting to Single: enter image description here

CodePudding user response:

First, make sure Resources.Load("candy_110/candy1_green_01") return you Object not null

  • Related