Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 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 /
avatar image
0
Question by Arkos25 · May 22, 2019 at 03:08 PM · if statementstringsignoreelsestatement

If statement is being ignored and else is run instead

Hey guys, I've been getting a certain issues with my code and I can't for the love of god figure out why this is happening. The code below is a part of a stringbuilder which shows the player how many of a certain item he has and how many he needs for crafting. The problem I have is that the if-statement is ignored completely and the else is run instead. I don't know why this is happening and it frustrates me a lot.

Without the else statement everything works as expected but when I add it, it breaks the whole damn code. It keeps showing 0/8 when it should be 15/8 because that is what I've set the item amount to. I tried throwing out Debug.Logs and for some reason it shows that the if statement is run once but it also shows that the else statement is run 34 times. A very specific number. Not sure why.

So I don't forget, this function is only run when the player clicks on an item he wants to craft. Its a OnClick event so it should only run once.

     public void ShowItemInfo()
     {
         for (int i = 0; i < slots.Itemslots.Length; i++)
         {
             if (Reci.MaterialsNeeded == 3)
             {
                 if (Reci.Materials[3].item.isequal(slots.Itemslots[i].Item))
                 {
                     sb3.Length = 0;
                     ShowItemInfo3(slots.Itemslots[i].Item.amount, Reci.Materials[3].amount);
                     ItemInfo[3].text = sb3.ToString();
                 }
                 else
                 {
                     sb3.Length = 0;
                     ShowItemInfo3(0, Reci.Materials[3].amount);
                     ItemInfo[3].text = sb3.ToString();
                 }
             }
         }
     }


The reason I wanted an else there is because if I don't have the item it's checking with I want it to instead just show the player that he has zero of that item.

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 Bunny83 · May 22, 2019 at 04:00 PM

You do realise that your for loop runs through all items in your itemslots? If the item you're looking for is at index 3 out of 24 you will enter the if body when "i" is 3 but for all following iterations (4, 5, ...23) you will enter the else part. Unless the item you're looking for is the last one you will always have the else part running last.


Your general structure is quite confusing. Why do you check if (Reci.MaterialsNeeded == 3) every iteration? It doesn't change in between. If it actually is changed by one of your used methods this would be a very (very) bad design. What you usually would do is something like this:

 public void ShowItemInfo()
 {
     if (Reci.MaterialsNeeded == 3)
     {
         sb3.Length = 0;
         ShowItemInfo3(0, Reci.Materials[3].amount);
         ItemInfo[3].text = sb3.ToString();
 
         for (int i = 0; i < slots.Itemslots.Length; i++)
         {
             if (Reci.Materials[3].item.isequal(slots.Itemslots[i].Item))
             {
                 sb3.Length = 0;
                 ShowItemInfo3(slots.Itemslots[i].Item.amount, Reci.Materials[3].amount);
                 ItemInfo[3].text = sb3.ToString();
                 break;
             }
         }
     }
 }

I guess sb3 is a StringBuilder? This is also bad design. It seems that ShowItemInfo3 does not actually show any information but actually fill the sb3 string builder with data. This is completely hidden and not obvious from the code. If you want to re-use the stringbuilder, just pass it as parameter to the method. Also you may want to find a better name for the method that actually describes what it does.


Also the name "ShowItemInfo3" suggests that there might be also a "ShowItemInfo2" and "ShowItemInfo1" method?!? This is also not very descriptive and also a general hint for a bad design decision.

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 Arkos25 · May 22, 2019 at 04:53 PM 0
Share

$$anonymous$$aterialsNeeded checks how many materials the item I want to craft requires.

I only have one ShowItemInfo method that runs the code above. Bad na$$anonymous$$g convention, I know, will fix it as soon as I have nipped this issue in the bud. below the above method I have the parameters the stringbuilder looks for. I have four, one for each item.

     public void ShowItemInfo0(int itemamount, int itemreq)
     {
         if (itemreq > 0)
         {
             sb0.Append(itemamount);
             sb0.Append("/");
             sb0.Append(itemreq);
         }
     }
     public void ShowItemInfo1(int itemamount, int itemreq)
     {
         if (itemreq > 0 && itemamount > 0)
         {
             sb1.Append(itemamount);
             sb1.Append("/");
             sb1.Append(itemreq);
         }
     }
     public void ShowItemInfo2(int itemamount, int itemreq)
     {
         if (itemreq > 0)
         {
             sb2.Append(itemamount);
             sb2.Append("/");
             sb2.Append(itemreq);
         }
     }
     public void ShowItemInfo3(int itemamount, int itemreq)
     {
         if (itemreq > 0)
         {
             sb3.Append(itemamount);
             sb3.Append("/");
             sb3.Append(itemreq);
         }
     }


How would I go by fixing it if I had four items I needed to check. if I do it as you suggested above then I would need a for loop inside every if statement that checks $$anonymous$$aterialsNeeded. I would like to apologize for not pasting all the code from the beginning. Sorry.

I would also like to add that I can at most have an item require 4 separate materials in order for the player to be able to craft it. At the least it is 1 material needed for crafting. The above code I pasted shows only one item, one material....I'm making this more confusing than it is aren't I.

avatar image Arkos25 · May 22, 2019 at 06:12 PM 0
Share

Yeah, The for loop didn't even cross my $$anonymous$$d to be honest. But I can see now why it was a bad idea to have the check inside of the loop and not vice versa. I'm far from being adept at program$$anonymous$$g but I hope I can get there someday. Your suggestion worked, though now I have one for loop inside every if statement that checks "$$anonymous$$aterialsNeeded". So four for loops in total. It works as I wanted and there are no issues with it anymore but how optimized is this? is it O$$anonymous$$ to have four for loops inside a single $$anonymous$$ethod?

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

107 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

Related Questions

if else statement doesnt work 1 Answer

Destorying despite IF Statement 1 Answer

How to make several conditions for an if statement? 5 Answers

'else' won't work in an if statement 1 Answer

Why is one line of code correct but the other gives Errors? (Solved) 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