• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Trollvahkiin · Oct 12, 2014 at 06:55 PM · arrayspriteinventory

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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")) {
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Trollvahkiin · Oct 12, 2014 at 07:35 PM 0
Share

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?

avatar image jkpenner · Oct 12, 2014 at 08:40 PM 0
Share

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")) {

avatar image Trollvahkiin · Oct 12, 2014 at 08:57 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

29 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

put every sprite frame in a array 1 Answer

One component in Array which is a class resets itself to null. 1 Answer

Scroll List Problem 1 Answer

how do I scroll thru a array 2 Answers

Error when trying to save inventory with array. 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges