Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question image.sprite not changing during run time

Discussion in 'Scripting' started by Flynn_Prime, Oct 11, 2021.

  1. Flynn_Prime

    Flynn_Prime

    Joined:
    Apr 26, 2017
    Posts:
    383
    Code (CSharp):
    1.     public void UpdateSlot(Item slotItem)
    2.     {
    3.         Item = slotItem;
    4.         image.sprite = Item.sprite; // Image not getting changed ...
    5.  
    6.         if (Item.isStackable)
    7.             count.text = Item.amount.ToString();
    8.         else
    9.             count.text = "";
    10.     }
    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?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,710
    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:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494
     
  3. Flynn_Prime

    Flynn_Prime

    Joined:
    Apr 26, 2017
    Posts:
    383
    Code (CSharp):
    1. public void UpdateSlot(Item slotItem)
    2.     {
    3.         Item = slotItem;
    4. Debug.Log(Item.sprite.name);
    5.         image.sprite = Item.sprite; // Image not getting changed ...
    6.         if (Item.isStackable)
    7.             count.text = Item.amount.ToString();
    8.         else
    9.             count.text = "";
    10.     }
    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
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,710
    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.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,710
  6. Flynn_Prime

    Flynn_Prime

    Joined:
    Apr 26, 2017
    Posts:
    383
    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...
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,710
    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.
     
    Flynn_Prime likes this.