- Home /
How to check the current sprite in the image source? Unity 4.6beta20
Hey, so I'm making an inventory and I need to check if the image source in the image component is "Blank_Icon" which is an actual sprite. So I added the Blank_Icon sprite to the Resources and the code goes like this:
public void AddItemToInventory(string itemName)
{
for (int i = 0; i < 2; i++) {
if (invIcons[i].GetComponent<Image>() == Resources.Load("Blank_Icon")) {
print("Found an empty space!");
break;
}
}
}
This doesn't work thought so how could I make it work? Thanks!
Answer by jkpenner · Oct 12, 2014 at 07:10 PM
It appears you're comparing an Image class to a sprite. The Image contains a sprite, so the following should do what you want:
if(invIcon[i].GetComponent<Image>().sprite == Resources.Load("Blank_Icon")) {
That's great but now I noticed another problem which is it doesn't work when I use Resources.Load("Blank_Icon")
. It worked when I simply made a variable dedicated to the Blank_Icon. Am I loading it in wrong?
First, make sure the "Blank_Icon" is in a resources folder. I forgot about the type cast for the Resources.Load, since it returns an Object.
Updated the line with the type cast, I believe this should be right.
if(invIcon[i].GetComponent().sprite == (Sprite)Resources.Load("Blank_Icon")) {
Ah great, it was close but the actual line was
if (invIcons[i].GetComponent<Image>().sprite == Resources.Load<Sprite>("Blank_Icon")) {
Thanks for the help!
Your answer
Follow this Question
Related Questions
put every sprite frame in a array 1 Answer
inventory system issues 0 Answers
Inventory system using an array... 3 Answers
Setting generic list length 2 Answers
Inventory moving items bug. 0 Answers