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 TeftyTeft · Aug 21, 2014 at 07:26 PM · editorprefabinspectorprocedural

Assigning a dynamically created prefab to a ScriptableObject

Hello,

I'm creating some tools in the editor to make life easier, but I've run into a road block. I'm procedurally generating a prefab and a ScriptableObject based on a GameObject that I've selected in the scene when I'm working. (it's for platform generation/creation)

The method gets called via a menu item link.

When I assign the newly created prefab to the ScriptableObject field that holds the reference to the prefab (in code), it attaches it to the GameObject in the scene, and not the Prefab in the Asset Database. And the inspector says "mismatch type".

Does anybody know how to do this? I'm trying to remove the step of dragging and dropping the prefab into the ScriptableObject every time because I'm going to be doing this a lot.

EDIT: The code being executed is in the #if UNITY_EDITOR namespace so instantiate will not work.

Thank you!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by TeftyTeft · Aug 21, 2014 at 09:22 PM

Okay, I figured it out and wanted to share for anybody running into the same problem.

You have to reload the saved prefab using AssetDatabase.LoadAssetAtPath and reference that GameObject for your ScriptableObject instance.

   string prefabLocation = "Assets/prefabSaveLocation/" + Selection.activeGameObject.name + ".prefab";
             Object prefab = PrefabUtility.CreateEmptyPrefab(prefabLocation);
         PrefabUtility.ReplacePrefab(Selection.activeGameObject, prefab, ReplacePrefabOptions.ConnectToPrefab);
 
         CustomObject platform = ScriptableObject.CreateInstance<CustomObject> ();
 
         GameObject prefabLoad = AssetDatabase.LoadAssetAtPath (prefabLocation, typeof(GameObject)) as GameObject;
 
         platform.platformPrefab = prefabLoad;
 
         AssetDatabase.CreateAsset (platform, "Assets/scriptableObjectLocation/" + Selection.activeGameObject.name + ".asset");
         AssetDatabase.SaveAssets ();
 
         EditorUtility.FocusProjectWindow ();
         Selection.activeObject = platform;

`

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
avatar image
0

Answer by winxalex · Feb 14, 2018 at 02:22 PM

  AssetDatabase.AddObjectToAsset(prefab, platform);
 platform.platformPrefab=prefab;
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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Referencing / linking a .asset / .prefab file in another .asset / .prefab file programmatically. 2 Answers

How to know if something is selected in Hierarchy or in project. 5 Answers

How to set a shared material in inspector? 0 Answers

How do I get the default scene GUI for a CustomEditor for RectTransform? 1 Answer

Can I force z position to match prefab when it's dragged onto scene? 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