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
Answer by M-Hanssen · Apr 18, 2016 at 01:42 PM
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);
}
}
I think your error might be because of this line:
img = (RawImage)GameObject.Find("Image").GetComponent();
I think what you want to do is this:
img = newbutton.GetComponentInChildren();
Answer by Blademaster680 · Apr 18, 2016 at 01:54 PM
Yes, my intentions were to have a class there. I have quite a few levels so that keeps it in an array like this:
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
What is your exact error? Because this code works just fine:
[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 LevelsController : $$anonymous$$onoBehaviour
{
public RawImage Image;
public Level[] Levels;
protected void Start()
{
Image.texture = Level.LevelTexture;
}
}
I think your error might be because of this line:
img = (RawImage)GameObject.Find("Image").GetComponent<RawImage>();
I think what you want to do is this:
img = newbutton.GetComponentInChildren<RawImage>();
That worked!
Thank you so much!!!
It was the:
img = (RawImage)GameObject.Find("Image").GetComponent<RawImage>();
That needed to be changed to:
img = newbutton.GetComponentInChildren<RawImage>();
Np!
I added the solution to my answer above, could you mark it as valid answer please?
Your answer
Follow this Question
Related Questions
New to Unity and Design, need some help :( 1 Answer
Image attached to the image component in a button is not visible during play mode 0 Answers
Trouble with controlling the direction of projectiles with C# code 1 Answer
How to get access to other's script var with increment? 0 Answers
UnityEngine.UI and UnityEngine.EventSystem do not work, what do I do? 1 Answer