Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Jamoy1993 · Jan 22, 2014 at 02:02 AM · if-statementslogicconfused

Unity completely defys logic in this if statement.

Hello im having trouble with this piece of code im trying to formulate for an inventory system i got. I am making a system where if you have a bag on and you are carrying more items than your default inventory can hold, the bag cannot be unequiped.

             if(i.itemType == "Backpack") // if the item is a backpack
             {
                 Debug.Log("is backpack");
                 if(playersinv.Contents.Length < (playersinv.MaxContent - i.GetComponent(i.ItemName + "_script").BagExtraSlots) == true); 
 
 //Basically if the current inventory array contains less items than the default inventory
                 {
                     Debug.Log("Current Inventory Content" + playersinv.Contents.Length + " Bag total slots " + playersinv.MaxContent + " Extra slots " + i.GetComponent(i.ItemName + "_script").BagExtraSlots);
                     canUnequip = false; // stop me from unequiping.
                 }
             }
 

Basically my equation for detering whether or not the bag has enough space returns false to the following logical statement 0 < 2. Ive even substitued in 5 < 2 whic it totaly isnt and it still thinks it = true!?

What the hell am i doing wrong? All i want it to do is

 if(CurrentArrayofObjects.Length > CurrentBagSize - AddedBagSlots)
 {
     CanUnEquip = false;
 }

even if i add If(blablabla == true)

it still spazzes out....

Any help???

Comment
Add comment · Show 1
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 VildNinja · Jan 22, 2014 at 02:18 AM 0
Share

Semicolon after second if statement. Also never write == true in an if statement. That is implicit. Not sure if this solves the problem.

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by AndyMartin458 · Jan 22, 2014 at 02:28 AM

The semicolon is separating your if statement from the block {}. Regardless of if the statment evaluates to true or false, you will enter the block.

You also should use parentheses to explicitly show what it is you are comparing. if(playersinv.Contents.Length < (playersinv.MaxContent - i.GetComponent(i.ItemName + "_script").BagExtraSlots) == true); // Wrong, don't use a semicolon

 if(  (playersinv.Contents.Length < (playersinv.MaxContent - i.GetComponent(i.ItemName + "_script").BagExtraSlots)) == true)
 {
 
 }
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 Jamoy1993 · Jan 22, 2014 at 11:05 AM 0
Share

Thanks i will give this a try

avatar image AndyMartin458 · Jan 22, 2014 at 05:39 PM 0
Share

@Jamoy1993 Did that turn out to be the answer?

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

20 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

Related Questions

False = true, what am I doing wrong? 2 Answers

How to call unity event once every time my object reach destination in Update()? 1 Answer

For loop inside if statement? 3 Answers

Check if all values in a list are equal 1 Answer

Problem comparing contents of arrays 1 Answer


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