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
1
Question by AmefuriSkeith · Jun 03, 2015 at 11:03 PM · editorprefabeditor-scriptingreferencemissingreferenceexception

How to keep reference linked when replace prefab through editor scripts?

Hi,

Currently I'm woking on some editor code where I have .asset contain all the link to my prefab and these prefab can be modify/replace through another editor script.

Let's say I have a set of prefab : PrefabA, PrefabB, PrefabC

All of these prefab were referenced by ScriptableObject asset

 public class AssetData : ScriptableObject 
 {
     public List<GameObject> prefabList;
 }

   

and I have another Editor script to "Replace" these prefab.

 GameObject replaceGameObject    = new GameObject("TEMP");
 Object prefab = AssetDatabase.LoadAssetAtPath(prefabPath, typeof(GameObject));
 if(prefab == null)
 {
    prefab = PrefabUtility.CreateEmptyPrefab(prefabPath);
 }
 PrefabUtility.ReplacePrefab(replaceGameObject, prefab);
 GameObject.DestroyImmediate(replaceGameObject);

The problem is after I finished replace the prefab, The reference in AssetData.asset (also happen to normal scene object serialization as well) above become a missing reference.

Did anyone ever experience this kind of situation?

Thank you,

Comment
Add comment · Show 3
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 Mastec · Jun 05, 2015 at 04:00 AM 0
Share

It seems like your trying to reference prefabs that no longer exist because they were destroyed. Have you tried adding your new prefabs to the list in AssetData? I might be wrong but that's what it looks like to me.

avatar image AmefuriSkeith · Jun 05, 2015 at 04:52 AM 0
Share

Right, Reference was being destroy but by being replace with another same name(same instanceID, same UDID too I think) prefab kinda strange since it appear that you can keep this reference alive when you did it by hand.

For ex. if you have AssetData link to PrefabA . and you drag a another gameobject from the scene to replace that PrefabA. A reference in AssetData still alive.

Of course I can try to re-reference it again if I have only 1 AssetData but since you may also have another scene/prefab/assetdata reference to these prefab anywhere anytime without being tracked by the editor code, this may not be a right choice for me.

and what I did destroy in the code was a temporary game object in the scene after I finish with the prefab replacement. So the prefab is still there.

avatar image jdean300 · May 18, 2017 at 07:10 AM 0
Share

Have you tried the different ReplacePrefabOptions that can be passed in to ReplacePrefab? For something similar that I did, ReplacePrefabOptions.ConnectToPrefab was the magic one, along with calling AssetDatabase.SaveAssets(); AssetDatabase.Refresh();

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by NathanJSmith · Dec 03, 2018 at 03:55 AM

I see the option: ReplacePrefabOptions.ReplaceNameBased keep reference linked when replace prefab through editor scripts

The script should be

 PrefabUtility.ReplacePrefab(source, dest, ReplacePrefabOptions.ReplaceNameBased);

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Created Prefabs and added them into a List in Editor Mode, after PlayMode, these GameObject are not in the List anymore 0 Answers

MonoBehavior Prefab modified by script in Editor loses changes when Play is hit 1 Answer

reconnect programmatically instantiated prefab 1 Answer

Could not load copied asset via AssetDatabase.LoadAssetAtPath 4 Answers

Set prefab to public variable of another script 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