Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 thegrandmaster97 · Jul 13, 2020 at 08:51 PM · listcopy

What is the easiest way to deep copy a list of gameObjects?

How can i make the "A" list have a different memory adress then the "B" one?

 List<UnitSegmentScript> A = new List<UnitSegmentScript>();
 foreach (GameObject x in B)
             {
                 A.Add(x);
             }
Comment
Add comment · Show 4
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 UnityedWeStand · Jul 13, 2020 at 09:05 PM 0
Share

Deep copy means that, not only will "A" have a different memory address than "B", but the objects inside A will be distinct clones from the objects inside B. Any changes to objects inside A will not affects any changes to objects inside B. Is this desired behavior?

Also, I assume UnitSegmentScript is a custom script attached to a GameObject?

avatar image thegrandmaster97 UnityedWeStand · Jul 14, 2020 at 06:35 AM 0
Share

Yes that is what i want to happen i want a distinct clone and yes UnitSegmentScript is a custom script attached to a GameObject

avatar image xxmariofer thegrandmaster97 · Jul 14, 2020 at 06:41 AM 1
Share

you need to clone the gameobjects before simply adding them to the list using Instantiate

          foreach (GameObject x in B)
          {
               GameObject clone = Instantiate(x);
               A.Add(clone );
          }
Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by UnityedWeStand · Jul 14, 2020 at 10:12 PM

Ok, if I am understanding your logic, you have:

- A list of GameObjects B , each with a ScriptData component attached to them
- A GameObject combatUnit , which has a UnitSegmentList component attached to it
- A function in UnitSegmentList called SetValues() that takes in one parameter of type List<ScriptData>

You want to send all of the ScriptData information into the SetValues() function of the combatUnit in a way that any modifications to the list data in combatUnit does not in any way affect the data already stored in B

This is a rather difficult problem, primarily because ScriptData inherits from MonoBehaviour , which means that you can't create a copy of an instance of ScriptData without attaching it to a GameObject, and you would need to create a copy of every GameObject in B and keep them in scene to achieve your desired results on your current setup.

An easy solution would be to store any information in ScriptData in a single Dictionary/Array/HashTable, depending on the nature of the data that needs to be passed into combatUnit . For example, if you had a list of stats:

 public class ScriptData : MonoBehaviour
 {
      public int[] stats;
     
      // whatever else is in the class
 }


You could then pass in clones of the stats variables into list A

 List<int[]> A = new List<int[]>();
 foreach (GameObject x in B)
 {
     A.Add(x.GetComponent<ScriptData>().stats.Clone());
 }
 GameObject combatUnit = Instantiate(unit);
 combatUnit.GetComponent<UnitSegmentList>().SetValues(A);


In this way, any changes made to combatUnit will not be reflected in B

Comment
Add comment · 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
0

Answer by djmatthy · 2 days ago

This worked for me:

 var returnData = JsonUtility.FromJson<LevelWaveData>(JsonUtility.ToJson(data));
Comment
Add comment · 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

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

136 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

Related Questions

A node in a childnode? 1 Answer

Copying multidimensional lists [SOLVED] 1 Answer

When I add a gameObject from one list to another, it makes a new one instead of referring to the same one? 0 Answers

How to make a deep copy instead of a shallow one? 1 Answer

copy an list in c# 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