Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Lukiszlaku · Jan 13, 2019 at 04:17 PM · arrayobjects

Values are saved in every object in Array of objects

I have a problem with array of objects. After every loop in for loop every object(name, x, y) are saved in all of them.

To make things clearer: i loop through my Objects giving every one a set of specific values to be saved later in JSON. Soo on the first loop i have name = one, x =1, y=1 and its for the container[0] only. Then after second loop when i set name = two, x=2, y =2 for container[1] then the container[0] and every other that have any values set will have the same values as container[1]. Soo after few loops i don't have every value for every object saved but instead i have X ammount of duplicated values from the last loop.

Here's my code: https://pastebin.com/E7RBPrps

Thanks in advance for your help.

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 Alanisaac · Jan 13, 2019 at 06:26 PM

The problem is that you only ever have one instance of PlayerData. Every index in your array 1...N is storing a reference back to that same, single object. Instead, if you want each index in the array to store its own instance, you need to construct a new instance for each index in the array. From a code perspective, what that means is you should move these lines:


 PlayerData tempData = new PlayerData();
 tempData.name = "a";
 tempData.x = 0;
 tempData.y = 0;

So that they go within the first for loop to initialize the data, like so:


 for (int i = 0; i < FloorContainer.transform.childCount; i++)
 {
     PlayerData tempData = new PlayerData();
     tempData.name = "a";
     tempData.x = 0;
     tempData.y = 0;
     playerData[i] = tempData;
 }

Comment
Add comment · Show 1 · 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 Lukiszlaku · Jan 13, 2019 at 10:00 PM 0
Share

Thanks, that was exactly the answer i needed. I also modified it a little to get rid of the tempData and now it looks like this:


for (int i = 0; i < FloorContainer.transform.childCount; i++)
        {
            playerData[i] = new PlayerData();
            playerData[i].name = FloorContainer.transform.GetChild(i).name;
            playerData[i].x = FloorContainer.transform.GetChild(i).GetComponent
  
   ().x; 
            playerData[i].y = FloorContainer.transform.GetChild(i).GetComponent
   
    ().y; 
        }

   
  

And right now i try to understand how to save this objects array into JSON with simple JSON... But thanks man. You've been very helpfull!

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

179 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 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 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 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 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 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 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 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 avatar image avatar image avatar image

Related Questions

Json to array of objects C# 1 Answer

Assaigning objects in class 2 Answers

How to get an object (that is a prefab) from an array and instantiate it (C#) 1 Answer

Creating and destroying SELECTED objects and... sorting arrays?? 0 Answers

Having problems with custom objects in my array - BCE0019 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