• 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 Addyarb · Nov 24, 2015 at 12:35 PM · prefabprefabutility

Prefab Interference?

Greetings!

I'm having issues with my prefab being overwritten by another prefab in the same scene.

So what's going on here? I have a prefab called "Audio System" and another called "Player."

Each time I run the game, everything works fine. But then after I stop it, or randomly some time thereafter, the "Audio System" prefab turns red (loses connection?) and then the actual "Audio System" prefab in my prefabs folder is replaced by the player.

Here's the code I'm using to save the changes to Audio System.

This question could also be read as....

What is the proper way to save ALL changes to a prefab via code?

I'm looking for something like :

 PrefabUtility.GetPrefab("AudioSystem").ApplyChanges();

Thank you!

     void UpdateAudioSystemValues ()
     {
         //Create an empty prefab with the same values as our current one, and then replace our old one with the new one.
         AudioSystem ASYSTEM = FindObjectOfType(typeof(AudioSystem)) as AudioSystem;
         Transform AS_Transform = ASYSTEM.transform;
         UnityEngine.Object prefab = PrefabUtility.CreateEmptyPrefab("Assets/AudioSystem/Resources/Prefabs/AudioSystem/AudioSystem.prefab");
         PrefabUtility.ReplacePrefab(AS_Transform.gameObject, prefab, ReplacePrefabOptions.ConnectToPrefab);
     }
Comment
Add comment · Show 1
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 _dns_ · Nov 24, 2015 at 03:25 PM 0
Share

Hi, I'm not sure I understand exactly what you want to do (editor code ? game running into the editor ?)

A simple way to edit prefab values by script is to have an object in the scene that holds a reference to a prefab: i.e. a $$anonymous$$anager object have a "public AudioSystem $$anonymous$$yAudioPrefab" that is assigned in the editor to the prefab that needs modifications (using drag & drop). Then, during the game or in editor code, just modify this object using "$$anonymous$$yAudioPrefab.property = value", then, after the modifications, call EditorUtility.SetDirty($$anonymous$$yAudioPrefab). Then you just have to save the scene (or stop the game and then save) to save modifications to the prefab.

How it works is that when an object has a reference to a prefab, Unity will create an instance of this prefab when the scene loads. This allows you to modify this instance as you want, then, using SetDirty(), you tell Unity to reflect those changes back to the prefab asset on the disc.

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

33 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

Related Questions

How to find the prefab base of a Prefab Variant through code? 1 Answer

Why does CreateEmptyPrefab return null? 1 Answer

How to check if an object is asset in build runtime (Not in Editor) ? 4 Answers

Editor Script: Linking GameObjects to public script variables resets when playing. 1 Answer

PrefabUtility.ApplyPrefabInstance throws error when there is Script that does not directly derive on MonoBehaviour 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