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 Tourist · Oct 03, 2014 at 02:36 PM · prefabassetscriptableobject

Add a GameObject as a sub-asset of a ScriptableObject

I am trying to add a GameObject as a sub-asset of a ScriptableObject.

Try #1 I have tried to do it that way :

 GameObject go = new GameObject("object name");
 go.hideFlags = HideFlags.HideInHierarchy;
 AssetDatabase.AddObjectToAsset(go, myAsset);

but when I press Play, I get those errors :

  1. Can't destroy Transform component of 'object name'. If you want to destroy the game object, please call 'Destroy' on the game object instead. Destroying the transform component is not allowed.

  2. Some objects were not cleaned up when closing the scene. (Did you spawn new GameObjects from OnDestroy?)

Try #2 and that way :

 GameObject go= new GameObject("object name");
 GameObject prefab = PrefabUtility.CreatePrefab("Assets/temp.prefab", go);
 prefab.hideFlags = HideFlags.HideInHierarchy;
 AssetDatabase.AddObjectToAsset(prefab, myAsset);

With this code, I get an error on the call to AssetDatabase.AddObjectToAsset :

  • You may not change the path if an object is already peristent in another one

Is it possible to add a gameobject (as a prefab or not) as a sub-asset of a ScriptableObject?

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 Tourist · Oct 03, 2014 at 03:34 PM 0
Share

myAsset in the previous code samples is a ScriptableObject.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by mintman · Feb 06, 2015 at 01:31 PM

It could be difficult.

A prefab is another asset, hence the error that the object path can't be changed. It might be the case that a prefab, internally, works by storing each of the components that exist on the GameObject instead of the GameObject itself. This would make sense, and produce the error you saw upon trying to add the prefab to another asset.

You may be able to iterate through each component and add it separately to the asset, and then instantiate the GameObject from that.

I'd imagine that it would be easier to just have prefab assets exist beside your custom asset, and store a reference to the prefab. I believe this should work. If referencing the prefab explicitly doesn't work, then maybe reference by it's path and use ISerializationCallbackReceiver to load the GameObject upon Deserialization.

Comment
Add comment · Show 3 · 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 Tourist · Feb 06, 2015 at 03:37 PM 0
Share

It would be easier to point to prefabs saved elsewhere but unfortunately this is not a solution I would like to consider, since the prefabs are hidden from the user and should not be edited/renamed/deleted by user.

avatar image mintman · Feb 06, 2015 at 04:13 PM 0
Share

That makes a lot of sense. Since you need to put a prefab in an asset you may need to add each component separately, and write some code to instantiate a game object from the components you stored.

I've never tried adding components into an asset myself, but I imagine it would work. That would be something to test before trying my suggestion above.

Good luck!

avatar image Tourist · Feb 07, 2015 at 08:15 PM 0
Share

Unity does not allow the creation of components without having a game object.

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

27 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

Related Questions

Saving changes to the asset files for Prefab and Scriptable objects. 1 Answer

Why is it that I can make prefabs (which are assets) reference scene objects but not be able to do the same for ScriptableObjects? 1 Answer

ScriptableObject assets null on startup, then reappear? 1 Answer

Getting a prefab with javascript FindAsset (Solved) 0 Answers

Creating prefab from instance of a GameObject loses materials 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