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 imp903 · Jun 19, 2015 at 09:39 PM · javascriptarrayinventoryiterate

Ways to optimize this code? iterate through inventory

Ok, so the way I have it set up is that in this game you have to craft your own guns by collecting parts throughout the round and then going to the crafting bench and crafting the weapons. Anyway, trying to keep it as simple as possible because there is no need for a real "inventory" I just made the inventory a list of strings really. Here I want to be able to have one variable part of the craft process which is the power source. In the UI you click the power source, which brings up a list of possible power sources, which you can click one of. This then changes the UI text of the original screen from "no source selected" to whatever you chose. Then if that is changed, the light behind the text lights up. if all the lights have an opacity of 1 then the condition is met to craft. Here's the code, but the problem is that it's so clunky it only works to light up the light behind the power source every dozen times or so due to processing requirements (i think that's what it is, because it does ocassioanlly work). Here's the part of the code that checks if the text has changed which is really not very efficient I think. I left out some of the lines above but all the component references are solid.

 function Update ()
 {
     var sourceText : Text= powerSource.GetComponent.<Text>();
     var theText : String = sourceText.text;
     if (theText != noSourceText)
     {
         checkInventoryCores();
     }
 }
 
 function checkInventoryCores()
 {
     var sourceText : Text= powerSource.GetComponent.<Text>();
     var theText : String = sourceText.text;
     var Bag = inventory.itemBag;
     var psLightA : float = powerSourceLight.GetComponent.<Image>().color.a;
     for (var j = 0; j<Bag.length; j++)
     {
         if (Bag[j] != null)
         {
             if (Bag[j] == "Luminescent Crystal" && theText == "Luminescent Crystal")
             {
                  psLightA = 1;
             } else if (Bag[j] == "Charged Cobalt" && theText == "Charged Cobalt")
             {
                 psLightA = 1;
             } else if (Bag[j] == "Hyperionized Plasma" && theText == "Charged Cobalt")
             {
                 psLightA = 1;
             } else if (Bag[j] == "Molten Core" && theText == "Molten Core")
             {
                 psLightA = 1;
             } else if (Bag[j] == "Radium Shard" && theText == "Radium Shard")
             {
                 psLightA = 1;
             } else if (Bag[j]=="Unidentifiable Spheroid" && theText == "Unidentifiable Spheroid")
             {
                 psLightA = 1;
             } else {
                 psLightA = 0;
             }
         }
     }
 }    
Comment
Add comment · Show 2
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 Dave-Carlile · Jun 19, 2015 at 09:54 PM 1
Share

I'm not really understanding exactly what's going on based on your description. Can you edit your question and add some additional info? $$anonymous$$aybe a couple of screenshots including one where it's working as expected?

That said, this doesn't look like it would have any huge performance impact, unless the inventory is really, really huge or something. Even with the GetComponent calls in Update (those should generally be saved in variables in Start so you don't have to call GetComponent each frame)

avatar image imp903 · Jun 19, 2015 at 10:51 PM 0
Share

Thanks for the feedback, because even though I didn't really have the question phrased correctly, you saying that this it probably wasn't the performance made me go back to check my script, and I realized that the way it iterates, unless the last inventory item is the power source, that last else line will always set the light behind it to off. That's why it was only working every so often because I picked up the items before running to the crafting table in a different order every time. So, indirectly you solved it, so thanks :)

0 Replies

· Add your reply
  • Sort: 

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

22 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

Related Questions

Loop Through Entire Array - Simon Game 1 Answer

"For"-Loop only works for final element in array 1 Answer

How to determine what type of class is being interated through in an array of multiple classes 1 Answer

Having trouble iterating through an array of GameObjects of a certain type 1 Answer

Shotgun using array/list in Javascript 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