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 rsoneriu · Oct 30, 2014 at 06:31 AM · objectspooling

Copy values from one object (scriptwise) to another

Hey guys, this might be a simple question but I couldnt find any related posts or questions. What I want to do is to make a script object the copy of another one. I'm trying to do this this way:

     public var test : List.<Objects>;
     public var localTest : List.<Objects>;
     
     function OnEnable(){
           localTest = test;
     }

Apparently this only sends a reference to the first object, thus everything I do with the second one is actually happening to the first one. I am afraid that if i loop through the first one and add the values to the second one, it will also only save references to the first one.

Any help is much appreciated

Comment
Add comment · Show 5
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 fafase · Oct 30, 2014 at 06:47 AM 0
Share

http://stackoverflow.com/questions/222598/how-do-i-clone-a-generic-list-in-c

avatar image rsoneriu · Oct 30, 2014 at 06:49 AM 0
Share

Yeh thanks, i did see that but i am not writing it in c#. I Could conevrt it but i was hoping there is an out of the box option for unity script also

avatar image AlwaysSunny · Oct 30, 2014 at 07:06 AM 0
Share

So you want to copy the elements of the list into a new list? Huh. Don't suppose you could...

foreach (Object obj in oldList) newList.Add(Instantiate(obj));

avatar image rsoneriu · Oct 30, 2014 at 07:09 AM 0
Share

Thank you all for your answers, I think i will go for instantiating the object the contains it (and destroying). Since i will do this only every 15 seconds or so i hope it wont affect that much the performance of the game. And i will use pooling for the smaller assets.

avatar image Baste · Oct 30, 2014 at 09:19 AM 0
Share

I can guarantee you that you do not need to use pooling. Seriously. You're in a learning stage, and you should not spend time on learning to optimize before you learn to actually write working code.

It keeps infuriating me that tutorials thinks that object pooling belongs on the same level as say copying a list. They don't. There's months of learning between those two points.

If you don't have a grasp on the difference between objects and references to objects work - as your question shows - do yourself a favor and skip the object pooling for now.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by tanuj0092 · Oct 30, 2014 at 01:09 PM

Once a List is created, it references the values from different memory locations. Eg. if you create an integer list then list[0], list[1]..etc actually points the value at that memory location, not the actual value. The instruction localtest = test actually passes the data of those memory locations to another list. So, what will happen, is when you edit the localtest list, it will automatically reflect in test list as well.

So, you need to create a new list and copy all elements in a loop as

 for( int i = 0; i < test.count; i++ )
 {
     localtest.Add(test[i]);
 }













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

30 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

problem with my simple script please help. 2 Answers

NullReferenceException: Object reference not set to an instance of an object Raycast...? 1 Answer

Why Does My GameObject Not Spawn Anymore? 2 Answers

How may I get the children (direct and dependents) of a game object? 2 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