Level Selector - Level Images for Brick Breaker

I am trying to make a level selector scene where players unlock levels and it displays a picture of the level so they can track their progress.

I have done the level unlocker and selector system, Im stuck on the images being displayed on the Raw Image component.

I have got the level properties in a class, they all load perfectly fine. I need the ‘LevelTexture’ ​to be in that class too.

If the ‘LevelTexture​’ are not in the class then it loads fine, but as soon as I move the variable into the class it no longer works.

I have a feeling its got something to do with (Texture). if its ‘(Texture)LevelImage’, it works find, but as soon as I put it in the class it now needs to be ‘level.LevelTexture’. and I have no idea where to put the (Texture) part now.

​​​

    public class Level
    {
        public string LevelText;
        public int LevelID;
        public int Unlocked;
        public bool IsInteractable;
        public Texture LevelTexture;
        public Button.ButtonClickedEvent OnClickEvent;
    }



   // public Texture LevelTexture;

void Start ()
{
    levelManager = GameObject.FindObjectOfType<LevelManager>();
    FillList();
}

void FillList()
    {
        foreach (var level in LevelList)
        {
            //Level button creation and name
            GameObject newbutton = Instantiate(levelButton) as GameObject;
            LevelButton button = newbutton.GetComponent<LevelButton>();
            button.LevelText.text = level.LevelText;
            img = (RawImage)GameObject.Find("Image").GetComponent<RawImage>();


        //Level locked/unlocked
        if (PlayerPrefs.GetInt("LevelsUnlocked") >= level.LevelID)
        {
            print("Unlocked" + level.LevelID);
            level.Unlocked = 1;
            level.IsInteractable = true;

            img.texture = level.LevelTexture;                //This line has the issue.

          //img.texture = LevelTexture;

            print("Image loaded: " + level.LevelTexture);
            //  LevelPeak.sprite = level.LevelImage;
        }

        //Button Update
        button.unlocked = level.Unlocked;
        button.GetComponent<Button>().interactable = level.IsInteractable;
        button.GetComponent<Button>().onClick.AddListener(() => levelManager.LoadLevel(button.LevelText.text));

        newbutton.transform.SetParent(Spacer, false);
    }
}

​​

If anyone can help it would be greatly appriciated

You now have a class inside you class! Is this your intention?

If this IS your intention, this means that your class should be serializable and should be placed outside the MonoBehaviour class.

Example:

[Serializable]
public class Level
{
    public string LevelText;
    public int LevelID;
    public int Unlocked;
    public bool IsInteractable;
    public Texture LevelTexture;
    public Button.ButtonClickedEvent OnClickEvent;
}

public class LevelSelector : MonoBehaviour
{
    public Level Level;

    // public Texture LevelTexture;
        private void Start()
        {
        }
    
}

Is this is NOT intention you should leave out the bracket ;-p

public class Level : MonoBehaviour
     {
         public string LevelText;
         public int LevelID;
         public int Unlocked;
         public bool IsInteractable;
         public Texture LevelTexture;
         public Button.ButtonClickedEvent OnClickEvent;
 
 
    // public Texture LevelTexture;
 void Start ()
 {
     levelManager = GameObject.FindObjectOfType<LevelManager>();
     FillList();
 }
 void FillList()
     {
         foreach (var level in LevelList)
         {
             //Level button creation and name
             GameObject newbutton = Instantiate(levelButton) as GameObject;
             LevelButton button = newbutton.GetComponent<LevelButton>();
             button.LevelText.text = level.LevelText;
             img = (RawImage)GameObject.Find("Image").GetComponent<RawImage>();
 
         //Level locked/unlocked
         if (PlayerPrefs.GetInt("LevelsUnlocked") >= level.LevelID)
         {
             print("Unlocked" + level.LevelID);
             level.Unlocked = 1;
             level.IsInteractable = true;
             img.texture = level.LevelTexture;                //This line has the issue.
           //img.texture = LevelTexture;
             print("Image loaded: " + level.LevelTexture);
             //  LevelPeak.sprite = level.LevelImage;
         }
         //Button Update
         button.unlocked = level.Unlocked;
         button.GetComponent<Button>().interactable = level.IsInteractable;
         button.GetComponent<Button>().onClick.AddListener(() => levelManager.LoadLevel(button.LevelText.text));
         newbutton.transform.SetParent(Spacer, false);
     }
 }

Yes, my intentions were to have a class there.
I have quite a few levels so that keeps it in an array like this:

68296-levels-controller.png

There you will also be able to see that I am storing the Level texture image in the editor

There you will see once I click play, it loads the level selection menu, but not the images. The Texture is still blank on the Image.

This is what I dont understand, its not setting the texture and I cant figure out why
It loads the names and everything else perfectly fine, its just those images