Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by qdrien · Sep 07, 2016 at 12:47 PM · shaderprefabmaterial

Create a prefab from an instantiated object in the editor

Hello,

I'm trying to create a prefab from an object at runtime in the editor and I'm having issues. The idea is to create the prefab from a script (usingPrefabUtility.CreatePrefab(...)) but as dragging & dropping the item does not work either, I'll simply explain the issue using this "manual" process because it seems easier to reproduce.

  1. In the editor, if I drag & drop my fbx model, change materials/shaders, then drag & drop in the prefabs folder, everything works fine.

  2. Now, in my use case, a gameobject is created "dynamically" using the same fbx model and the materials/shaders are also modified by script to get the exact same object as in step 1. If I press "Pause" in the editor before dragging & dropping the object in the prefabs folder, the generated prefab is broken (pink texture as no materials/shaders were saved).

The only differences I see between the GameObjects from both steps are that the second one (step 2) is obviously named "myObject(Clone)" with its shaders named "shaderName (Instance)" (as it was instantiated from a script) and that the shaders are in reverse order in the components list (which should not be a problem).

To clarify, here are some screenshots (uploaded outside of this website because a question can apparently contain only 2 attached images):

http://www.noelshack.com/2016-36-1473241780-chairs.jpg

http://www.noelshack.com/2016-36-1473241780-chair-inspector-working.jpg

http://www.noelshack.com/2016-36-1473241780-chair-prefab-working.jpg

http://www.noelshack.com/2016-36-1473241780-chair-inspector-notworking.jpg

http://www.noelshack.com/2016-36-1473241780-chair-prefab-notworking.jpg

Any help would be much appreciated, feel free to ask for clarifications if needed.

======= Edit =======

Here is a minimal (non-)working example:

https://mega.nz/#!HwQyXC4Y!_Tr736a7lHkI5M5O0P6F5obkrddH1qBrFeDD2S8MZm0

If you don't want to download the project, here is the code used in it:

 using UnityEngine;
 using UnityEditor;
 
 public class Test : MonoBehaviour
 {
     void Start () {
         Object chairObject = Resources.Load("ChairA");
         GameObject chairGameObject = (GameObject) Instantiate(chairObject, 
             new Vector3(-.5f, 0, 0), Quaternion.identity);
         chairGameObject.transform.Rotate(0,250,0);
 
         //Removing the following line make the prefab "work" for some reason
         Material[] materials = chairGameObject.GetComponent<Renderer>().materials;
         //Could modify materials/shaders here but simply getting a reference 
         //to the list of materials seems to be a problem to save the prefab...
 
         PrefabUtility.CreatePrefab("Assets/Resources/SavedPrefabs/test.prefab", 
             chairGameObject);
         GameObject prefab = Resources.Load("SavedPrefabs/test") as GameObject;
         GameObject chairPrefab = (GameObject) Instantiate(prefab, 
             new Vector3(.5f, 0, 0), Quaternion.identity);
         chairPrefab.transform.Rotate(0, 250, 0);
     }
 }


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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by qdrien · Sep 08, 2016 at 08:55 AM

Apparently, I did fix the issue myself, by replacing:

 Material[] materials = chairGameObject.GetComponent<Renderer>().materials;

by:

 Material[] materials = chairGameObject.GetComponent<Renderer>().sharedMaterials;

I can now modify materials/shaders using this "shared reference" and then create the prefab which seems to be saved properly. I won't delete the question in case someone faces the same (strange, at least to me) issue in the future.

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

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

Related Questions

Sorting (rendering) order of materials in mesh renderer 1 Answer

I can see the objects through the terrain. 1 Answer

Material.SetTexture doesn't work if not "_MainTex" 0 Answers

Shader Forge outputting gray instead of black 1 Answer

Shader displaying with weird colors 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