Swipe the UI.image with different sprite[Solved]

Hello,

I do have a “custom button” with animations and when I click it, I want it based on specific condition change the image of the UI.Image component. The button is not that default button. It’s made out of Button->Panel->Image.

So, I’ve attached to this button a script with public variables for the sprites that I want to change and for the Image which has to be changed with those sprites:
The code:

    public Sprite[] _onOff; //[0] - is On sprite' [1] - is the Off Sprite

    public Image _imgToSwipe; // image to swipe component
public void SwipeSprite()
    {
        if (MadLevelProfile.GetProfileBoolean("_isOff") == false)
        {
            MadLevelProfile.SetProfileBoolean("_isOff", true);
           _imgToSwipe.sprite = _onOff[1];
          
          
            Debug.Log("Im inside the IsOff trueeee");
        }
        else
        {
            MadLevelProfile.SetProfileBoolean("_isOff", false);
            _imgToSwipe.sprite = _onOff[0];
            Debug.Log("Im inside the IsOff falseeee");
        }
    }

Whenever I click on that button I can see in console the code running perfectly. In both states, like “off” and “on” but the sprite on the Image is not changing at all. The console also doesn’t give any errors… Could you help me what’s wrong here?

And of course in the editor I’ve attached all necessary sprites and image object to the public variables so they are not null…

when are you calling that function? Also assuming you are using the correct nameSpaces, UnityEngine.UI?

Typically for changing the sprite, I would use the OnPointerDown interface to check when that image is clicked and then sprite swap it.

Make sure you have a proper handle to the image from the variable _imgToSwap.

As I said, its a button. That function assigned to the button. So, whenever I click, I call that function. Just normal UI button onClick call…
Yes, I do use correct namespace, otherwise I couldn’t declare type of Image.

What do you mean about proper handle for the image?

I’ve been looking all over the internet. My code should work without any problem… Can someone else with Unity 5.0.1 or 5.0.2 try to see, if you can change the image sprite? I’m thinking it can be a bug? I’m lost…

i am using 5.0.2
and done it for you

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class UIEvent : MonoBehaviour
{
    public Image currentSrpites;
    public Sprite[] sprites;

    void Update()
    {
        if(Input.GetKeyDown(KeyCode.Alpha1))
        {
            currentSrpites.sprite = sprites[0];
        }

        if(Input.GetKeyDown(KeyCode.Alpha2))
        {
            currentSrpites.sprite = sprites[1];
        }

        if(Input.GetKeyDown(KeyCode.Alpha3))
        {
            currentSrpites.sprite = sprites[2];
        }
    }
}

2143072–141335–ChgImg.unitypackage (6.96 KB)

1 Like

@manlaikin1994 ,

Thats strange… Your scene/code works, but mine doesn’t… There are no difference between my approach and yours…

public void SwipeSprite()
    {
        if (MadLevelProfile.GetProfileBoolean("_isOff") == false)
        {
           _imgToSwipe.sprite = _onOff[1];
            MadLevelProfile.SetProfileBoolean("_isOff", true);
        
        
            Debug.Log("Im inside the IsOff trueeee");
        }
        else
        {
            _imgToSwipe.sprite = _onOff[0];
            MadLevelProfile.SetProfileBoolean("_isOff", false);
            Debug.Log("Im inside the IsOff falseeee");
        }
    }

what i guess the solution is put your " do sth" be4 set boolean to true to make it success

what i guess your last work is, u even doesn’t show the message from the debug.log

I’ve made a screenshot. I do actually show the debug in the Console, also the settings for the button.
Your last suggestions, unfortunately doesn’t change anything.

do u want sth like this?

2143096–141337–ChgImg.unitypackage (8.89 KB)

I do appreciate your help @manlaikin1994 , but my code is exactly the same as yours. It’s simply in my scene doesn’t working. I have no idea why… You can see in the screenshot…

btw, does anyone know how to convert it to ternary operator? want to learn sth;)

    public Image currentSrpites;
    public Sprite[] sprites;
    public bool isOn;

    void Update()
    {
        if(isOn == true)
        {
            currentSrpites.sprite = sprites[0];
        }
        else
        {
            currentSrpites.sprite = sprites[1];
        }
    }

try to set Boolean to false/true in the start function

and try to debug.log the status of the bool

I’ve put one more Debug.Log to see if the imgToSwipe.sprite.name changes to the ones which I have assigned from the editor. According to the console, it DOES change… but for some reason in the screen it doesn’t change. I can even change the Image color to the one which i want, but unfortunately the sprite is not changing. However, according to the console, the sprites have been changed but only not being showed on the screen.

@manlaikin1994 , Pls, pay attention on the console output which I’ve posted. It does show that IF statements works perfect as it should. It has nothing to do with bool variable. This part works awesome. The issue is something different.

ahh, my stupid, i knw what my problem is. sorry

you can chg ur sprite name, color, except img through the same scripts. Its weird

For whoever is interested. I solved it. The solution was to re-create that button again from scratch… And all code, everything started working as it should. It looks like a bug, where sometime the UI image renderer doesn’t actually renders correctly… It took me the whole 1 day, to get it fixed…
Unity you are making me crazy…

lol, i normally always rebuild project. but i guess u tried it.
Or it doesn’t work? it must be recreate?

I didn’t re-created the whole scene/project. Just that particular button which had problems. I also saw in the forum that other ppl have this issue too, where the image doesn’t renders correctly on UI objects.

i mean open the project again through “” File → open project “”
anyways, its good that the problem is solved although it doesn’t make sense