Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Blademaster680 · Apr 18, 2016 at 01:30 PM · c#unity 5ui2d gameui image

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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);
      }
  }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image M-Hanssen · Apr 18, 2016 at 02:32 PM 0
Share

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();
avatar image
0

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:

alt text

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

alt text

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


loading-images.png (107.9 kB)
levels-controller.png (18.5 kB)
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image M-Hanssen · Apr 18, 2016 at 02:20 PM 1
Share

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>();
avatar image Blademaster680 M-Hanssen · Apr 18, 2016 at 02:23 PM 0
Share

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>();

 
avatar image M-Hanssen Blademaster680 · Apr 18, 2016 at 02:33 PM 0
Share

Np!

I added the solution to my answer above, could you mark it as valid answer please?

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

165 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Image attached to the image component in a button is not visible during play mode 0 Answers

New to Unity and Design, need some help :( 1 Answer

Values not updating in Unity. - UPDATED 1 Answer

RectTransform Left Right Bottom Top 0 Answers

Canvas not filling up the whole screen in Unity 5 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges