• 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 mrBeam · Mar 19, 2014 at 01:18 PM · instantiate prefab

PrefabUtility.InstantiatePrefab returns Null in Editor

When I use regular Instantiate it returns the selected object I selected in the editor window and creates clones for me. BUT I want to use the PrefabUtility.InstantiatePrefab so every clone keeps the prefab connection. Problem is that PrefabUtility.InstantiatePrefab returns Null for me. You can easily try both Instantiate and PrefabUtility.InstantiatePrefab by removing my comments in the code below.

Thank you for your time.

 using UnityEditor;
 using UnityEngine;
  
 public class MyWindow : EditorWindow
 {
     public GameObject useGameObject;
     
     [MenuItem ("Tools/Replace %g")]
     public static void Replace()
     {
         EditorWindow.GetWindow(typeof(MyWindow));
     }
         
     void OnGUI ()
     {
         GUILayout.Label ("Use Object", EditorStyles.boldLabel);
         useGameObject = EditorGUILayout.ObjectField(useGameObject, typeof(GameObject), true) as GameObject;
         if (GUILayout.Button ("Replace Objects"))
         {        
             foreach (Transform t in Selection.transforms)
             {
                 Debug.Log(useGameObject);
                 // BELOW DOES NOT WORK
                 //GameObject newObject = PrefabUtility.InstantiatePrefab(useGameObject) as GameObject;
                 // BELOW WORKS
                 GameObject newObject = Instantiate(useGameObject) as GameObject;
                 Debug.Log(newObject);
                 
                 Transform newT = newObject.transform;
                 newT.position = t.position;
                 newT.rotation = t.rotation;
                 newT.localScale = t.localScale;
                 newT.parent = t.parent;
             }
         }
     }
 }
Comment
Add comment · Show 2
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 mrBeam · Mar 19, 2014 at 02:22 PM 0
Share

Did you comment out the line with the PrefabUtility before running? And yes I drag a prefab, from the hierarchy list in to the object field thus assigning it to useGameObject.

And thank you for helping!

EDIT: Now I understand that there is a difference between scene objects and prefabs.

avatar image eightbitstev · Mar 19, 2014 at 03:13 PM 0
Share

Yeah, if you're dragging from the Hierarchy, you are grabbing a scene gameobject, even if it is blue. If it's blue, it just means its a scene gameobject instanced from a prefab, but isn't actually the prefab itself. You'll need to reference it from the project window, but I'm assu$$anonymous$$g that you've already made it that far since you got it working! :) I'm converting my comment to an answer since this seems to be the push-in-the-right-direction you needed to get your question answered.

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by eightbitstev · Mar 19, 2014 at 01:32 PM

I just tried out your code and it works perfectly for me. Is it possible that you've assigned useGameObject to a scene object instead of a prefab by accident?

EDIT: If you're dragging from the Hierarchy, you are grabbing a scene gameobject, even if it is blue. If it's blue, it just means its a scene gameobject instanced from a prefab, but isn't actually the prefab itself. You'll need to reference it from the project window.

Comment
Add comment · Show 1 · 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 mrBeam · Mar 19, 2014 at 03:35 PM 0
Share

Thank you for pushing me in the right direction. I've posted the code that works now. This time it works with prefabs, scene objects and objects!

avatar image
2
Wiki

Answer by mrBeam · Mar 19, 2014 at 03:25 PM

Got it to work! Now it can handle scene objects, prefabs and objects! Enjoy =)

 using UnityEditor;
 using UnityEngine;
  
 public class ReplaceSel : EditorWindow
 {
     GameObject myObject;
     
     [MenuItem ("Tools/ReplaceSelected %g")]
     public static void ReplaceObjects() {
         EditorWindow.GetWindow(typeof(ReplaceSel));
     }
         
     void OnGUI () {
         GUILayout.Label ("Use Object", EditorStyles.boldLabel);
         myObject = EditorGUILayout.ObjectField(myObject, typeof(GameObject), true) as GameObject;
         if (GUILayout.Button ("Replace Selected")) {
             
             if (myObject != null) {
                 foreach (Transform t in Selection.transforms) {
                     GameObject o = null;
                     o = PrefabUtility.GetPrefabParent(myObject) as GameObject;
                     
                     if (PrefabUtility.GetPrefabType(myObject).ToString() == "PrefabInstance") {
                         o = (GameObject)PrefabUtility.InstantiatePrefab(o);
                         PrefabUtility.SetPropertyModifications(o, PrefabUtility.GetPropertyModifications(myObject));
                     }
                     
                     else if (PrefabUtility.GetPrefabType(myObject).ToString() == "Prefab") {
                         o = (GameObject)PrefabUtility.InstantiatePrefab(myObject);
                     }
                     
                     else {
                         o = Instantiate(myObject) as GameObject;
                     }
                     
                     Undo.RegisterCreatedObjectUndo(o, "created prefab");
                     Transform newT = o.transform;
                     newT.position = t.position;
                     newT.rotation = t.rotation;
                     newT.localScale = t.localScale;
                     newT.parent = t.parent;
                     
                     foreach (GameObject go in Selection.gameObjects) {
                         Undo.DestroyObjectImmediate(go);
                     }
                 }
             }
         }
     }
 }
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.

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

21 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

Related Questions

Move bullet in the same direction of mouse 1 Answer

Object Pooling issue. GameObject instantiating 1 bullet instead of multiple bullets 1 Answer

Button not instantiating, no errors 2 Answers

Trying to implement a cone shot in a 2D Game. 1 Answer

instantiate objects within an area 2 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