image.sprite not changing during run time

    public void UpdateSlot(Item slotItem)
    {
        Item = slotItem;
        image.sprite = Item.sprite; // Image not getting changed ...

        if (Item.isStackable)
            count.text = Item.amount.ToString();
        else
            count.text = "";
    }

As above, i’m trying to change an image to a sprite at runtime. The sprite is referenced in “Item” which is a scriptable object. I am not getting any null references, so i’m not sure why my sprite does not change! Can anyone offer some insight please?

You must find a way to get the information you need in order to reason about what the problem is.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

public void UpdateSlot(Item slotItem)
    {
        Item = slotItem;
Debug.Log(Item.sprite.name);
        image.sprite = Item.sprite; // Image not getting changed ...
        if (Item.isStackable)
            count.text = Item.amount.ToString();
        else
            count.text = "";
    }

I did a fair amount of debugging prior to my post, and only decided to post here after I tried the above debug. The debug displays the name of the correct sprite that should be displaying also, so how is it even possible that this is happening?

For now, this particular method is only called when an InventorySlot is first created and using Debug.Log the method is called 3 times on Start() (which is exactly what should be happening as I add 3 items to the inventory through code initially); but still no sprite displayed. Its almost as if the reference to the item.sprite is lost between the various method calls. Could possibly be related to my delegates as my InventoryUI relies on events to trigger changes. I’ll do some more debugging tomorrow. Thank you

Here’s three ways off the top of my head:

  • this script isn’t even on the GameObject you think it is

  • what you think is the image is actually some other image

  • something else is forcing the image back to the original value AFTER your code above runs

etc.

When all else fails, delete it and rebuild it… likely you will reveal the issue.

Just noticed this: if you’re creating / assigning those delegates in a loop, keep this in mind:

Delegate / Action variable capture / value capture:

https://discussions.unity.com/t/809602/2

This is great, I learned something new atleast! But, was not the issue unfortunately.

I redragged the image fields in the inspector on my slot prefab and it’s now working. Baffled is not the word, as the references were definitely exactly the same as before…

If you use source control you can actually see it was probably different… and it was likely some odd artifact of how the scene / prefab was put together, or some kind of linkage confusion inside the scene / prefab. You can get this if you change ONLY the type of a public variable without renaming it. Unity gets confused as to what you’re serializing and makes mistakes that it commits.

1 Like