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 Rabwin · May 20, 2013 at 01:10 PM · gameobjectcheckpoint

Save a copy of a gameObject

I'm trying to create a simple save/reload system, where objects of many different types need to be saved. There are many objects with different scripts, and I need copies of those instances with all their variables in whatever state they had at the time of copying.

Setting a GameObject variable with a gameObject in the scene only makes a reference to that object, not a state copy. (i.e if it gets destroyed or altered, the variable is affected).

pseudocode to show my problem:

 GameObject objectReference = find("a gameobject");
 GameObject objectCopy = objectReference;
 destroy (objectReference);
 print(objectCopy); > console prints "null"

How can I store a copy of a gameObject? Am I going to have to write a custom class to store all the possible data in an instance of a gameObject? If I have to do this, would it be possible to use typecasting? This method would be quite a pain :(

Thanks!
PS: using C#

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
0
Best Answer

Answer by flamy · May 20, 2013 at 01:14 PM

Gameobject is reference type. once u destroy the object source, the copy of reference is also deleted.

hence the value in objectCopy becomes null when destroy is called.

something once destroyed cannot be recreated as the exact same. what you want to store? positions? scripts? or some other component?

Why not disable the object instead of destroying it. So that you can re-enable it later?

or you can serialize the object and save it to local and get that back later on to create similar object (have a look at Unity Serializer for that purpose)

Comment
Add comment · Show 5 · 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 Rabwin · May 20, 2013 at 01:18 PM 0
Share

How can I make a copy ins$$anonymous$$d of a reference? Any tips?

avatar image flamy · May 20, 2013 at 01:24 PM 0
Share

updated the answer.

avatar image Rabwin · May 20, 2013 at 01:27 PM 0
Share

I want to store a copy of objects that can be moved by the player, and have states (stored in different scripts) that can also be changed by the player. I need to be able to move them, destroy them, and re-create them when the player decides to reload the level from when they saved. Simply disabling the object won't do as I still need the object so that the player can use it until they reload, or save again (and i'd overwrite the stored state).

avatar image Rabwin · May 20, 2013 at 01:29 PM 0
Share

Actually, disabling the object gives me a good idea. What if I create a clone and disable it immediately, and destroy the original object when the player "reloads" then enable the clone. Although, this doesn't allow me to save states in between games (i.e the player closing the game and then loading it back up) I guess I need to write a custom serializer to extend the Unity Serializer.

avatar image Rabwin · May 20, 2013 at 01:39 PM 1
Share

I'm going with using Unity Serializer with my own serialisation class for each custom monobehaviour I have that needs to be stored, and store it as a supported type in the Unity Serializer (a string). Thanks for the help :)

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

14 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

Related Questions

Save gameobject 1 Answer

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

The name "Destory" Does not exist in the current context 2 Answers

Moving object to specific location 1 Answer

Place gameobject on ROI 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