Reputation: 3
There are two graphical buttons on the canvas (UI->Button). By clicking on the second one, the following script is executed:
But1 = GameObject.FindGameObjectWithTag("ButtonEgg");
But1.GetComponent<Image>().sprite = Resources.Load<Sprite>("Eggs02");
It should replace the value in the first button in the Image component from the default Eggs01 sprite with Eggs02. But instead it gives a sprite none and a white rectangle. Both pictures are in the Sprites folder, Sprite (2D and UI) is set in the inspector, and Single is set in Sprite mode.
Tried to duplicate the sprite in the Resources folder. Even tried to specify the path:
Resources.Load<Sprite>("Sprites/Eggs02");
up: Debug.Log(Resources.Load("Sprites/Eggs02")); issues null but for sure the sprite is in the folder and even tried to copy the path through the context menu still null (((
I'm not a magician, I'm just learning, so I strongly ask you not to kick and help the young man solve the problem.)
Upvotes: 0
Views: 156
Reputation: 90659
Resources.Load
only works with assets placed in and a path relative to a folder called Resources
... So unless your sprite is placed in e.g. Asset/Resources/Sprites/Eggs02
it will obviously return null
.
In general you don't want to use Resources
at all!
Rather have a
public Sprite sprite;
// or
//[SerializeField] private Sprite sprite;
and drag your sprite into the slot in the Inspector and then simply do
But1.GetComponent<Image>().sprite = sprite;
Upvotes: 0