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 rgowen · May 15, 2014 at 02:46 AM · c#objectclonecopy

Getting a copy/clone of an object from an array (C#)

I'm currently working on an inventory and item management system that uses a sort of loot table system for all possible items. The loot "table" is quite simple, it's just an array of my InventoryItem objects that I want to define in the Inspector. Hypothetically, if I wanted to add one of those items to my player's inventory, through some sort of function like this:

 inventory.AddItem(lootTable[1]);

It would just pass the reference to the same object in memory, so both my inventory class and my loot table class would be pointing at the same object. Is there an easy way to clone that object, short of making an entirely new function to copy all the parameters to a new one?
Or, if there's a better way to implement a loot table like this I'm all ears.

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 robertbu · May 15, 2014 at 02:47 AM 0
Share

What are the entries of 'lootTable' if they are game objects or components on game objects, you can just use Instantiate() to make a copy.

avatar image rgowen · May 15, 2014 at 02:57 AM 0
Share

lootTable contains InventoryItem objects (C# objects, not GameObjects or Components).
They contain all the information needed to make a GameObject and its Components (say, if the item was dropped into the world and we need to create a GameObject), however they don't have the ability to create that GameObject. They also do not extend $$anonymous$$onoBehaviour.

avatar image robertbu · May 15, 2014 at 04:35 AM 0
Share

$$anonymous$$y usual solution to this situation is the thing you are trying to avoid. I add a Copy() method to the InventoryItem class and do:

  inventory.AddItem(lootTable[1].Copy());   
avatar image rgowen · May 15, 2014 at 04:37 AM 0
Share

Yeah, I wouldn't be surprised if that's what I'll end up doing. I'm messing around with C#'s $$anonymous$$emberwiseCopy(), which does a shallow copy and may be suitable for my needs. If it works, I'll post it as an answer.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by rgowen · May 16, 2014 at 03:20 AM

Answering my own question:

I ended up using C#'s MemberwiseClone() for this. It works fine for what I need. You should read how it works before implementing it yourself, as it is a shallow copy and will duplicate only the references to other objects within the object being cloned.

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 robertbu · May 16, 2014 at 03:21 AM 0
Share

Thanks for answering your own question. I learned something I'll use in the future.

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

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

How to refer to objects, scripts, textures in Assets in C#? 0 Answers

Array empties values on RunTime? 1 Answer

Angry Birds Style Loader (Load the Birds to be fired) 0 Answers

Getting all objects inside a Trigger 1 Answer

Trouble grabbing and holding a cube. 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