Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Barsonax · Oct 10, 2015 at 10:50 PM · variablesstringloopiterate

Executing variables as code?

So i have this piece of code that is working like i want it to but its rather cumbersome. I wonder if there is a way to do this with a loop since al the lines are kinda the same except for some minor differences. If there is a way to use variables for these minor differences so i dont have to copy them manually it would be very nice.

Here is the code:

 public void UpdateMenu(){
         Delay = 0;
 
         if (CurrentScroll > (Player.Inventory.Count-1)){
             Line1.text = "Nothing here scrub.....";
         }
         else {
             Line1.text = Player.Inventory[0+CurrentScroll].amount + " units of " + Player.Inventory[0+CurrentScroll].name;
             ResSp1.sprite = IDB.itemDB[0 + CurrentScroll].itemsprite;
         }
 
         if (CurrentScroll > (Player.Inventory.Count-2)){
             Line2.text = "-----------------------";
         }
         else {
             Line2.text = Player.Inventory[1+CurrentScroll].amount + " units of " + Player.Inventory[1+CurrentScroll].name;
             ResSp2.sprite = IDB.itemDB[1 + CurrentScroll].itemsprite;
         }
 
         if (CurrentScroll > (Player.Inventory.Count-3)){
             Line3.text = "-----------------------";
         }
         else {
             Line3.text = Player.Inventory[2+CurrentScroll].amount + " units of " + Player.Inventory[2+CurrentScroll].name;
             ResSp3.sprite = IDB.itemDB[2 + CurrentScroll].itemsprite;
         }
 
         if (CurrentScroll > (Player.Inventory.Count-4)){
             Line4.text = "-----------------------";
         }
         else {
             Line4.text = Player.Inventory[3+CurrentScroll].amount + " units of " + Player.Inventory[3+CurrentScroll].name;
             ResSp4.sprite = IDB.itemDB[3 + CurrentScroll].itemsprite;
         }
 
         if (CurrentScroll > (Player.Inventory.Count-5)){
             Line5.text = "-----------------------";
         }
         else {
             Line5.text = Player.Inventory[4+CurrentScroll].amount + " units of " + Player.Inventory[4+CurrentScroll].name;
             ResSp5.sprite = IDB.itemDB[4 + CurrentScroll].itemsprite;
         }
 
         if (CurrentScroll > (Player.Inventory.Count-6)){
             Line6.text = "-----------------------";
         }
         else {
             Line6.text = Player.Inventory[5+CurrentScroll].amount + " units of " + Player.Inventory[5+CurrentScroll].name;
             ResSp6.sprite = IDB.itemDB[5 + CurrentScroll].itemsprite;
         }
 
         if (CurrentScroll > (Player.Inventory.Count-7)){
             Line7.text = "-----------------------";
         }
         else {
             Line7.text = Player.Inventory[6+CurrentScroll].amount + " units of " + Player.Inventory[6+CurrentScroll].name;
             ResSp7.sprite = IDB.itemDB[6 + CurrentScroll].itemsprite;
         }
 
 
         if (CurrentScroll > (Player.Inventory.Count-8)){
             Line8.text = "-----------------------";
         }
         else {
             Line8.text = Player.Inventory[7+CurrentScroll].amount + " units of " + Player.Inventory[7+CurrentScroll].name;
             ResSp8.sprite = IDB.itemDB[7 + CurrentScroll].itemsprite;
         }
 
 
         if (CurrentScroll > (Player.Inventory.Count-9)){
             Line9.text = "-----------------------";
         }
         else {
             Line9.text = Player.Inventory[8+CurrentScroll].amount + " units of " + Player.Inventory[8+CurrentScroll].name;
             ResSp9.sprite = IDB.itemDB[8 + CurrentScroll].itemsprite;
         }
 
 
         if (CurrentScroll > (Player.Inventory.Count-10)){
             Line10.text = "-----------------------";
         }
         else {
             Line10.text = Player.Inventory[9 + CurrentScroll].amount + " units of " + Player.Inventory[9 + CurrentScroll].name;
             ResSp10.sprite = IDB.itemDB[9 + CurrentScroll].itemsprite;
         }
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

1 Reply

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

Answer by Statement · Oct 10, 2015 at 11:54 PM

 // Remove variables Line1 .. Line10 and ResSp1 .. ResSp10
 // Use lists instead.
 public List<Text> Lines = new List<Text>();
 public List<Image> ResSp = new List<Image>();
 
 public void UpdateMenu()
 {
     for (int i = 0; i < 10; ++i)
         UpdateMenuEntry(i);
 }
 
 private void UpdateMenuEntry(int i)
 {
     var line = Lines[i];
     var resSp = ResSp[i];
     var itemId = i + CurrentScroll;
     var item = Player.Inventory[itemId];
 
     if (CurrentScroll > (Player.Inventory.Count - (i + 1)))
     {
         line.text = "-----------------------";
     }
     else
     {
         line.text = item.amount + " units of " item.name;
         resSp.sprite = IDB.itemDB[itemId].itemsprite;
     }
 }

And if you want, you could group up the UI objects...

 // If a Text and an Image belong together, make a class for them
 [System.Serializable]
 public class InventoryItemUI
 {
     public Text line;
     public Image res;
 
     public string Text { get { return line.text; } set { line.text = value; } }
     public Sprite Sprite { get { return res.sprite; } set { res.sprite = value; } }
 
     public void Clear()
     {
         Text = "-----------------------";
         // Sprite = blank?
     }
 
     public void Refresh(int itemId)
     {
         // I dont know if Player is a variable or a class.
         // I dont know if IDB is a variable or a class.
         var item = Player.Inventory[itemId]; 
         Text = item.amount + " units of " item.name;
         Sprite = IDB.itemDB[itemId].itemsprite;
     }
 }
 
 public List<InventoryItemUI> inventoryItemUI = new List<InventoryItemUI>();
 
 public void UpdateMenu()
 {
     for (int i = 0; i < inventoryItemUI.Count; ++i)
     {
         var item = inventoryItemUI[i];
         if (CurrentScroll > (Player.Inventory.Count - (i + 1)))
             item.Clear();            
         else
             item.Refresh(i + CurrentScroll);}
     }
 }
Comment
Add comment · Show 2 · 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 Barsonax · Oct 11, 2015 at 08:36 AM 0
Share

Wow thanks this seem to be what iam looking for.

I have one question though. In this situation i know how many text objects there will be beforehand. Isnt it better in that case to use a normal array ins$$anonymous$$d of a list?

avatar image Statement Barsonax · Oct 11, 2015 at 08:38 AM 0
Share

Apples and pears... Do as you wish.

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

31 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

Related Questions

Changing a value over time 1 Answer

Loop variables (Names of Inputfields / Labels) 1 Answer

,Code in loop simultaneously executing on members of array rather than iterating one at a time 1 Answer

Accessing a non-static string variable in another script C# 1 Answer

what is a string ? i dont understand please help 2 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