- Home /
Change UI Image through a script with AssetDatabase.LoadAssetAtPath
Hi, I'm creating a battle interface for my game on which every turn different cards are chosen randomly and displayed to the player. So I generate a random number bewteen 0 and 9, since I have 10 cards for now (it will be over 100 when finished). Then I proceed to change the Image from the gameobject in the UI so that the corresponding image is shown according to the selected card. (Only four cards can be displayed every turn.
The problem here is that I don't know how to change the Image from the UI object by looking for the asset, I would create a array and assign them, but since in the end there is going to be over 100 cards I believe it would not be the best way to do it. I tried this:
void OnEnable()
{
int chosenCard;
//int[] usedNumbers;
//For every available slot
for (int i = 0; i <= numberOfSlots-1; i++) {
chosenCard = Random.Range (0,9);
cardSlots [i].GetComponent<Image> ().sprite = (Sprite)AssetDatabase.LoadAssetAtPath ("Assets/Images/HandCards_Placeholder - 10 cards/HandCards_Placeholder - 10 cards_" + chosenCard, typeof(Sprite));
}
}
But when I run the game no image is shown, the field on the Image component is empty. I guess the issue here is that I'm not using correctly the method. Hope you can help.
Thanks in advance.
Your answer
Follow this Question
Related Questions
Problem with Image and Sprite 1 Answer
How create a audience style game Rocksmith 1 Answer
Canvas image sprite change from script 0 Answers
Cast object as sprite for UI Image 1 Answer
How can I get Image size? 0 Answers