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.
CodePudding user response:
You can try changing the Sprite Mode
import setting to Single
:
CodePudding user response:
First, make sure Resources.Load("candy_110/candy1_green_01") return you Object not null