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 darkironphoenix · Nov 14, 2014 at 09:28 PM · listif

Getting a result of an if statement from a list.

So, i have a list of items like so

     var itemList = new Item[40];
      
     class Item {
     var itemName : String;
     var PrereqItem1 : String;
     var PrereqItem2 : String;
     var sellPrice : int;
     var weight : float;
     var quantityInInventory : int;
     }

And in a seperate file i have a function which, on fired, i would like to return the value of an item based off the two items the player has selected. So far the function looks like this:

 function SearchList(){
 
 
 for (var i : int = 0; i < Inv.itemList.length; i++) {
 
     if (item1 == Inv.itemList.PrereqItem1 && item2 == Inv.itemList.PrereqItem2){
     Result = Inv.itemList.itemName;
     }
     }
 
 }


So, as an example, the player might select a fish (item 1) and a pizza (item 2), and click the button. The code will then search the list for an item that has both the prerequisites of pizza and fish, then return the name of "fish pizza".

My issue is that i cannot work out how to word the if statement so it...well...works. I suspect the answer is simple, but i'm currently staring at the wood and wondering where the trees are.

Any help you guys can give will be much appreciated.

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
1
Best Answer

Answer by robertbu · Nov 14, 2014 at 09:30 PM

The typical solution would be to return null if something is not found:

 function SearchList() : String {
     for (var i : int = 0; i < Inv.itemList.length; i++) {
 
         if (item1 == Inv.itemList[i].PrereqItem1 && item2 == Inv.itemList[i].PrereqItem2){
             return Inv.itemList[i].itemName;
         }
     }
     return null;
 }

You can then check for null as the return value to see if the function succeeded or failed.

Comment
Add comment · Show 9 · 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 darkironphoenix · Nov 14, 2014 at 09:34 PM 0
Share

Ok,so the errors i'm getting now is this: Assets/Assets/Scripts/Player Systems/Crafting.js(80,27): BCE0019: 'PrereqItem1' is not a member of 'Item[]'. One for item name and prereq2 as well...

avatar image robertbu · Nov 14, 2014 at 10:04 PM 0
Share

You cannot access non-static variables in another file like you are attempting unless they are static. Research static variables, GetComponent(), and Singletons:

https://docs.unity3d.com/352/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html

http://wiki.unity3d.com/index.php?title=Singleton

Also you may want to reconsider how you are structuring things. It seems to be that the SearchList() function belongs with the Item class (though you would still have to figure out how to access this method from another file).

avatar image darkironphoenix · Nov 14, 2014 at 10:11 PM 0
Share

Gah, ok, ill have a plot and see what i can come up with. Thanks for the help though!

Wait...so if i combined the two files, theoreticlly it should work fine?

avatar image robertbu · Nov 14, 2014 at 10:41 PM 0
Share

If you combine the two files, then yes you can access the list. You will be accessing 'itemList', not 'Inv.itemList'.

avatar image Bunny83 · Nov 15, 2014 at 12:38 AM 1
Share

@darkironphoenix: You missed the index on your list when you access an element inside the list. I've fixed the answer. He just copied your faulty code and hasn't spotted that one ;)

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

2 People are following this question.

avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

How do you know if a value is a list? 0 Answers

Increment based 'if' conditions not being met for the later conditions. Any solutions ? 0 Answers

If String is in List Then... 2 Answers

C# ArrayList match to string? 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