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 /
avatar image
0
Question by steelsean · Dec 09, 2017 at 07:20 PM · gameobjectprefabssaveload

Finding gameobjects prefab

HI, new to Unity.

I am working on saving the state of a simple 2d game that has runtime created game objects. I wanted to save the bare minimum and was thinking:

  1. Position

  2. Angle

  3. Prefab used

Position and Angle are easy. Where I am confused is how do I find whatever prefab was used to create the gameobject? I was hoping there would be an easy filed to get the prefab used to instantiate the object but I can't seem to find one meant for production. Of course, I need whatever reference I can save to be able to be used to load those prefabs later.

Worst case I can assign unique IDs manually by attaching a script component but since there might be hundreds of prefabs that would be a non-elegant solution that I would like to avoid.

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

Answer by Bunny83 · Dec 09, 2017 at 10:59 PM

The concept of prefabs do not exist at runtime. At runtime a prefab is just an ordinary gameobject with the exception that it is not part of the rendering list of the scene but only exists in memory. When you use Instantiate you just create a clone of the source object which is placed into the scene. Instantiate can clone any UnityEngine.Object. The instantiated object has no information where it came from.


Generally if you want to serialize the information of the source prefab you have to store that information inside the cloned object yourself. The best way is usually that you have your prefabs in a globally accessible array and you simply store the index of the prefab. Another way, if indices might be too messy or the number of prefabs might change you can also use the prefab name as long as it's unique, You would do the same and keep a list of all prefabs available but to access them you could create a Dictionary to do a name lookup.


Generally when you instantiate an object you may want to store the required information in the newly created clone. Something like this:

 GameObject clone = Instantiate(prefabs[prefabIndex]);
 clone.GetComponent<YourScript>().prefabIndex = prefabIndex;
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

103 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

Related Questions

Create with code 2.2 pizza not shooting 1 Answer

Hello I have a script in which saving a character position but how can I save a text value also or gameobject? when the player save the game? This is my code for saving the position of the player. 0 Answers

Mark gameobject field as changed from prefab 2 Answers

How to tell at runtime if a GameObject is a prefab 3 Answers

how to get the prefab path of a game object in Game mode,not Editor mode 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