Changing Panel Source Image when a button is clicked

Ok I’m dealing with this for 2 days now, I’m feeling I’m missing a simple but an important point.

My aim is to change the source image of the panel of the new UI, which I’am using for background image, by clicking a button. There are 4 jpg background images in the Assets/Resources/Backgrounds folder, and Main Camera has a ChangeBackground script which has an OnClickChangeBackground method. The button has an onClick event to this method. I want the button to cycle between these 4 images. The problem is eventhough there are no console errors, nothing is happening when the button is clicked.

The ChangeBackground scripts is like this:

[*code=CSharp]
using UnityEngine;
using Assets;
using UnityEngine.UI;
using System.Collections;

public class ChangeBackground : MonoBehaviour
{
public static int currentSprite = 0;
public string resourceName = “Backgrounds”;
public Sprite[ ] backgrounds;

void Awake()
{
if (resourceName != “”)
backgrounds = Resources.LoadAll (resourceName);
}

public void OnClickChangeBackground()
{

if(currentSprite == 0)
{
GameObject.Find (“Panel”).GetComponent ().sprite = backgrounds [currentSprite];
currentSprite++;
}

if(currentSprite == 1)
{
GameObject.Find (“Panel”).GetComponent ().sprite = backgrounds [currentSprite];
currentSprite++;
}

if(currentSprite == 2)
{
GameObject.Find (“Panel”).GetComponent ().sprite = backgrounds [currentSprite];
currentSprite++;
}

if(currentSprite == 3)
{
GameObject.Find (“Panel”).GetComponent ().sprite = backgrounds [currentSprite];
currentSprite = 0;
}
}
}
[/code]

Please, use the tags [*code=CSharp][/code]…

Put a Debug.Log in your OnClickChangeBackground and check if you really call this function.

Check also if, when the game is launched, your background images are really set.

Sorry for not paying attention to code tags.

I’ve solved the problem, the sprite array is populated with sprites, there was nothing wrong there. The script’s begun to work after adding else if statements. Here’s the code:

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


public class ChangeBackground : MonoBehaviour
{
    public static int currentSprite = 0;
    public string resourceName = "Backgrounds";
    public Sprite[] backgrounds;


    void Awake()
    {
        if (resourceName != "")
                        backgrounds = Resources.LoadAll<Sprite> (resourceName);
        }


    public void OnClickChangeBackground()
    {

        if (currentSprite == 0)
        {
                        GameObject.Find ("Panel").GetComponent<Image> ().sprite = backgrounds [currentSprite];
                        currentSprite++;

                } else if (currentSprite == 1) {
                        GameObject.Find ("Panel").GetComponent<Image> ().sprite = backgrounds [currentSprite];
                        currentSprite++;
                } else if (currentSprite == 2) {
                        GameObject.Find ("Panel").GetComponent<Image> ().sprite = backgrounds [currentSprite];
                        currentSprite++;
                } else if (currentSprite == 3) {
                        GameObject.Find ("Panel").GetComponent<Image> ().sprite = backgrounds [currentSprite];
                        currentSprite = 0;
                }

        }
    }