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 /
This question was closed Jul 06, 2014 at 10:37 PM by QuestionAsker for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by QuestionAsker · Jan 22, 2014 at 08:29 PM · c#monodevelopglitchfor-loopfor

Glitch in Monodevelop for loops!!!

Predict the output before you read the bottom!

 private InventorySlot[] FillInventoryWithJunk (int Cols, int Rows, int storageType, InventoryItem junk, int[] skipSlots)
     {
         InventorySlot[] itemsStorage = new InventorySlot[Rows * Cols];
 
         for(int i = 0; i < itemsStorage.Length; i++)
         {
             itemsStorage[i] = new InventorySlot(junk);
             itemsStorage[i].Item.ID = i;
         }
 
         for(int i = 0; i < Rows * Cols; i++)
         {
             Debug.Log(itemsStorage[i].Item.ID);
         }
     
         return itemsStorage;
     }

Should the output not be 0, 1, 2, 3, 4, 5? What is actually output is "5, 5, 5, 5, 5". If the ID's were not changed it would output all 1's as thats what the "Junk" item ID is. If the debug.log is placed in the first loop it outputs correctly. If the ID assignment line is place in a separate loop, it still outputs not as expected in the debug loop. Ive been stuck on this for days. Any help would be amazing. Thanks in advance.

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

  • Sort: 
avatar image
2
Best Answer

Answer by njpatter · Jan 22, 2014 at 10:43 PM

You are setting the Item.ID on the same 'junk' object each time through. That's why it says 5,5,5,5,5 instead of 0,1,2,3,4.

Try copying/cloning/instantiating the prefab/dummy 'junk' object each time you want to add it to the array instead of using the same reference for all array elements.

Comment
Add comment · Show 6 · 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 QuestionAsker · Jan 23, 2014 at 08:18 PM 0
Share

Alight thank you, this is day 3 of this glitch so your response is greatly appreciated. I'm still trying to process this though. By how do I copy/clone it each time I add it to the array? I thought the new keyword did that. How would you go about doing this?

avatar image njpatter · Jan 23, 2014 at 08:25 PM 0
Share

Could you post the code from the InventorySlot constructor?

avatar image QuestionAsker · Jan 23, 2014 at 08:34 PM 0
Share
     public InventoryItem Item;
     public int Type; //0 is normal, 1 is hand
     
     //Constructors
     public InventorySlot (InventoryItem _Item)
     {
         this.Item = _Item;
         this.Type = 0;
     }
     public InventorySlot (InventoryItem _Item, int _Type)
     {
         this.Item = _Item;
         this.Type = _Type;
     }
avatar image QuestionAsker · Jan 23, 2014 at 08:37 PM 0
Share

I think you want to see the inventory item. Each slot holds an item. Just let me know and ill post that too!

avatar image njpatter · Jan 23, 2014 at 09:04 PM 1
Share

Ok, look at lines 7 and 12. When you pass the 'junk' object into the constructors the 'this.Item' is being set to a reference of the original 'junk' object.

This means that when you go to set the Item's ID, (0,1,2,3,4,5), the referenced object (which you are setting to be the same exact one by not copying/cloning it) has its ID changed. That is why they are all reporting '5' because you are asking the same object what its ID is 6 times (5,5,5,5,5,5).

To correct this you could copy the 'junk' item into a new instance of an InventoryItem in the InventorySlot constructor OR when you pass it to the constructor...

 itemsStorage[i] = new InventorySlot(new InventoryItem(junk));

That functionality may or may not be supported by the InventoryItem class but it should be an easy addition. Let me know if you've got any other questions

Show more comments

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

Autocomplete a for-loop in MonoDevelop? 1 Answer

Create a Pause In A For Loop 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Cant get Monodevolop to work 0 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