• 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 Tom Murray · Dec 31, 2013 at 09:53 PM · gameobjectinstantiateprefabobjects

How do I revert prefab instance properties to their defaults?

Hey,

I have an object pool manager class. It basically stores a bunch of prefabs (rockets, giblets, etc.) and manages instances of those prefabs to avoid the overhead of re-instantiating. Other functions can request instances, which follows this basic logic:

 /*** THIS IS PSEUDOCODE - just for showing concepts.  I know these won't actually work :)  ***/
 
 Dictionary <string, List <GameObject>> instanceDict;   // Instances stored here
 Dictionary <string, GameObject> prefabDict;  // Prefabs stored here
 
 // Spits out a requested instance
 public GameObject CheckOut (string name)
 {
   if (instanceDict [name].Count <= 0)
     instanceDict [name].Add ((GameObject) Instantiate (prefabDict [name]));
 
   GameObject returnObject = instanceDict [name][0];
   instanceDict [name].RemoveAt (0);
   Activate (returnObject);                   // Real code uses SetActive (true), etc.
   return returnObject;
 }
 
 // Places it back
 public void CheckIn (GameObject obj)
 {
   string name = GetObjectName (obj);
   instanceDict [name].Add (obj);
   Deactivate (obj);                        // Real code uses SetActive (false), etc.
 }

So the question is: How do I reset an already-existing instance\'s properties back to the prefab's, from within a running game? This is possible from within the editor, but to my knowledge there is no simple / actively encouraged way of doing this at runtime.

For instance: Say I have a "Robot Bee" prefab, which is, by default, a) in flight, b) not controlled by ragdoll physics and c) not dead. These are spawned from a hive and attack the player. However, once the player kills the bee, it will be dead, ragdolling, and not in flight for a bit before despawning automatically.

If that specific bee instance spawns again (which it probably will), it will be dead and ragdolling, unless I have a script specifically written to reset it.

I do not think this should be necessary, especially if one wants to have two varieties of bees controlled by slightly different prefabs ("warrior" bees, for instance, would fly faster, have more HP, and do more damage)

Is there a way to iterate through serialized properties of a prefab and one of its instances? I want to make as much of this game's logic manageable from inside the Unity Editor, and I am seeking a way to do this.

I know this is a tricky one - thanks in advance for the help.

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

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

19 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

Related Questions

How to instantiate within GameObject 1 Answer

Referencing gameObject from script after Instantiate 0 Answers

autolink to prefab? 0 Answers

Prefab instantiation: am I missing something? 1 Answer

How to make an Instantiated prefab a Child of a gameobject that already exists in the hierarchy 3 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