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 The Ghost · Aug 05, 2013 at 11:30 PM · loopinventoryforshop

How to make a for loop display inventory correctly?

I've been stuck on this issue a while now, I have a working inventory and shop system, however when I try to display it with GUI, it comes out wrong. Here's a pic of what's currently in the inventory(added at runtime from the shop).

alt text

Here is the script that's supposed to loop through it:

 for(loopInt = 0; loopInt < inventoryContents.Count; loopInt++)
         {
             InventoryItem inventoryItemScript = inventoryItem.GetComponent<InventoryItem>();
             if(targetPath.transform.childCount >= inventoryContents.Count)
             {
                 targetPath.transform.GetChild(targetPath.transform.childCount);    //Prevents clones
                 gridControlScript.Reposition();        //Organizes the inv with NGUI's grid
                 inventoryItemScript.UpdateInfo();    //Makes sure all the info is correct
             }
             else
             {
                 NGUITools.AddChild(targetPath, inventoryItem);
                 inventoryItemScript.item.itemName = inventoryContents[loopInt].itemName;
                 inventoryItemScript.item.icon = inventoryContents[loopInt].icon;
                 inventoryItemScript.UpdateInfo();        //Makes sure all the info is correct
                 gridControlScript.Reposition();            //Organizes the inv with NGUI's grid
             }
         }

NGUITools.AddChild is basically like instantiate but optimized for NGUI. gridControlScript.Reposition() is not important, just an NGUI thing that organizes the buttons.

Here is what inventoryItemScript.UpdateInfo() does:

 public void UpdateInfo()
     {
         itemNameTag = transform.Find("Item Text");
         itemNameTag.GetComponent<UILabel>().text = item.itemName;
         itemIcon = transform.Find("Texture");
         itemIcon.GetComponent<UITexture>().mainTexture = item.icon;
     }


I've checked and it does find the right Game Objects at runtime. However it doesn't seem to be getting the right info from the inventory for loop. Here's a pic in the inspector: alt text

What's even odder is that this is what I actually see in the game screen: alt text

All the info is completely different! And I have no idea why.

Comment
Add comment · Show 6
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 The Ghost · Aug 05, 2013 at 11:30 PM 0
Share

Drag the pictures into a new tab to see them more clearly.

avatar image perchik · Aug 06, 2013 at 10:18 PM 0
Share

Can you show the declaration for inventoryContents

avatar image boni · Aug 06, 2013 at 10:19 PM 0
Share
 InventoryItem inventoryItemScript = inventoryItem.GetComponent<InventoryItem>();

You're always getting the same Component, but you want to loop through all InventoryItems. If I remember correctly you can pass GetComponent an index-value, in your case loopInt.

avatar image boni · Aug 06, 2013 at 10:24 PM 0
Share

Also, if you're not sure WHY your script isn't doing what you want, put in some Debug-Logs so you can follow each step it does. Should help you to locate the problem more quickly. :)

avatar image DaveA · Aug 06, 2013 at 10:42 PM 0
Share

... or make friends with the debugger.

Show more comments

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

17 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

Related Questions

Why wont inventory wont update after or more purchases from in-game shop? 1 Answer

crazy loop !!! HELP ME 1 Answer

For loop not working 1 Answer

x=x Assignment made to same variable 3 Answers

In-game Shop / Market/ Buying / inventory system? 3 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