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]