Search Unity

Programmatically applied sprite not scaling correctly

Discussion in 'Editor & General Support' started by SanjayBhambhu, Jun 21, 2019.

  1. SanjayBhambhu

    SanjayBhambhu

    Joined:
    Aug 22, 2012
    Posts:
    13
    The script changes the gun sprite. When the script applies "assault-rifle" stripes, which are nearly the same size physically, there's no problem, but when the script applies the handgun sprite, it is stretched, even though the image has "preserve aspect" as selected.
    In the end of the video you can see that when I manually select the sprite (by selecting some other stripe and then selecting the main one again), the stripe is scaled correctly.
    Note: If I try to change the other options of the "Image" inspector, like "color", they don't even work, until I reselect the stripe manually.
    I am unable to understand what am I missing here.
    Please help.
    Thank you.

    How I am applying the sprite:
    Code (CSharp):
    1. public Image PrimaryWeaponIcon;
    2.  
    3. PrimaryWeaponIcon.sprite = newSprite;
     
    Last edited: Jun 22, 2019
  2. SanjayBhambhu

    SanjayBhambhu

    Joined:
    Aug 22, 2012
    Posts:
    13
    Update : It works when I assign using
    PrimaryWeaponIcon.overrideSprite
    instead of
    PrimaryWeaponIcon.sprite
    .
    Is there any specific reason?
    Also, is there any overhead of using overrideSprite for too many sprite changes? I know it keeps the first sprite in memory, does it keep all the subsequently assigned ones too?
    Thank You