Unity Community


Page 1 of 2 12 LastLast
Results 1 to 20 of 31

  1. Location
    SF Bay Area, CA, USA
    Posts
    269

    Replace game object with prefab?

    I know I can replace a prefab with a game object, but can I do the reverse? Meaning, I want to keep the same settings, at LEAST the transform, of the GO, but replace it with a prefab.

    Would make importing game objects to other scenes a little more do-able.

    Gotta be a way to script that.


  2. Location
    Amsterdam
    Posts
    436
    If I'm not mistaken, this is actually default behaviour. A prefab instance always has its transform settings as "overriden". So you can move the instance around, then change the prefab, and the instance will take on all settings from the prefab, except its transform settings.

    Bye, Lucas


  3. Location
    SF Bay Area, CA, USA
    Posts
    269
    I should have been more clear.

    I don't want to change settings on the prefab. I want to REPLACE the ENTIRE prefab with another.

    Example:
    I make a nice particle effect. I place this in the scene in several places. Then, my partner makes a better effect on his computer. I want to replace my particles with his, so I copy his prefab to my project.

    But now, there is no way for me (that I know of!) to replace my prefab with his.


  4. Location
    Phoenix, AZ
    Posts
    1,251
    Just drag an instance of his prefab from the scene hierarchy to the project pane, overriding your existing prefab.
    Matthew Wegner
    Current Project: Aztez
    Elsewhere:
    Founder, Flashbang Studios
    Partner, Indie Fund
    Editor, Fun-Motion
    Co-Chair, IGF


  5. Location
    SF Bay Area, CA, USA
    Posts
    269
    Thanks.

    Now the hard part: say you have a game object that contains one or more objects that you want to replace with prefabs. Think of them as place-holder objects. Now I want to replace the place-holders with prefabs, but without having to copy/paste all the params around. Is there a way to do that?


  6. Posts
    61
    Hey,
    i had the same problem and wrote a little script to fix it.
    Its realy not that hard as I thougt ^^

    What it does:
    The script lets you replace a hole bunch of placeholders at the same by an predefined Prefab.
    The object you put in for replacing needs to be a Prefab.

    If you want to replace many placeholders at the same time, attach them as children to another Object. For example an
    Empty. Then declare this Parent as the object to replace.
    The script will automaticly check for its children an replace them.

    using UnityEngine;
    using UnityEditor;
    using System.Collections;
    // CopyComponents - by Michael L. Croswell for Colorado Game Coders, LLC
    // March 2010

    public class ReplaceGameObjects : ScriptableWizard
    {
    public bool copyValues = true;
    public GameObject useGameObject;
    public GameObject Replace;

    [MenuItem ("Custom/Replace GameObjects")]


    static void CreateWizard ()
    {
    ScriptableWizard.DisplayWizard("Replace GameObjects", typeof(ReplaceGameObjects), "Replace");
    }

    void OnWizardCreate ()
    {
    Transform[] Replaces;
    Replaces = Replace.GetComponentsInChildren<Transform>();

    foreach (Transform t in Replaces)
    {
    GameObject newObject;
    newObject = (GameObject)EditorUtility.InstantiatePrefab(useGam eObject);
    newObject.transform.position = t.position;
    newObject.transform.rotation = t.rotation;

    Destroy(t.gameObject);

    }

    }
    }
    Feel free to do some improvements on the script. But please be fair enough to share it.
    So others could benefit of it.


  7. Posts
    1
    INSTANCE REPLACEMENT:

    Drag your new Prefab onto the existing GameObject in the Hierarchy while holding [Alt].
    Last edited by Ravael; 01-06-2011 at 09:17 AM.


  8. Location
    Denmark
    Posts
    23
    Hi

    I changed the script to allow for changing any number of gameobjects with a specific prefab.
    It also keeps the originals parents transform

    Code:  
    1. using UnityEngine;
    2. using UnityEditor;
    3. using System.Collections;
    4. // CopyComponents - by Michael L. Croswell for Colorado Game Coders, LLC
    5. // March 2010
    6.  
    7. //Modified by Kristian Helle Jespersen
    8. //June 2011
    9.  
    10. public class ReplaceGameObjects : ScriptableWizard
    11. {
    12.     public bool copyValues = true;
    13.     public GameObject NewType;
    14.     public GameObject[] OldObjects;
    15.  
    16.     [MenuItem("Custom/Replace GameObjects")]
    17.  
    18.  
    19.     static void CreateWizard()
    20.     {
    21.         ScriptableWizard.DisplayWizard("Replace GameObjects", typeof(ReplaceGameObjects), "Replace");
    22.     }
    23.  
    24.     void OnWizardCreate()
    25.     {
    26.         //Transform[] Replaces;
    27.         //Replaces = Replace.GetComponentsInChildren<Transform>();
    28.  
    29.         foreach (GameObject go in OldObjects)
    30.         {
    31.             GameObject newObject;
    32.             newObject = (GameObject)EditorUtility.InstantiatePrefab(NewType);
    33.             newObject.transform.position = go.transform.position;
    34.             newObject.transform.rotation = go.transform.rotation;
    35.             newObject.transform.parent = go.transform.parent;
    36.  
    37.             DestroyImmediate(go);
    38.  
    39.         }
    40.  
    41.     }
    42. }


  9. Posts
    620
    Great script... thanks...


  10. Posts
    114
    Many thanks. It has safe me a lot of work hours


  11. Posts
    1,887
    Thank you Kudos, this should be a standard Unity function imho...


  12. Posts
    2
    Heya guys,
    I'm having a hard time trying to get this to work in my situation. The alt drag doesn't work for either. It says that this is not implemented yet. As for the script it says it can't find the prefab in the prefab library. any ideas?


  13. Location
    London, UK
    Posts
    4
    Hey, I just registered on the community to say thank you to sunset and kudosforludos for this script. It totally worked and saved me a lot of bother


  14. Location
    Brisbane, Australia
    Posts
    492
    Excellent script. I modified it slightly so it copies scale and it prefills the selection

    Code:  
    1. using UnityEngine;
    2. using UnityEditor;
    3. using System.Collections;
    4.  
    5. // CopyComponents - by Michael L. Croswell for Colorado Game Coders, LLC
    6. // March 2010
    7.  
    8. //Modified by Kristian Helle Jespersen
    9. //June 2011
    10.  
    11. public class ReplaceGameObjects : ScriptableWizard
    12. {
    13.     public bool copyValues = true;
    14.     public GameObject NewType;
    15.     public GameObject[] OldObjects;
    16.  
    17.     [MenuItem("Custom/Replace GameObjects")]
    18.     static void CreateWizard()
    19.     {
    20.         var replaceGameObjects = ScriptableWizard.DisplayWizard <ReplaceGameObjects>("Replace GameObjects", "Replace");
    21.         replaceGameObjects.OldObjects = Selection.gameObjects;
    22.     }
    23.  
    24.     void OnWizardCreate()
    25.     {
    26.         //Transform[] Replaces;
    27.         //Replaces = Replace.GetComponentsInChildren<Transform>();
    28.  
    29.         foreach (GameObject go in OldObjects)
    30.         {
    31.             GameObject newObject;
    32.             newObject = (GameObject)EditorUtility.InstantiatePrefab(NewType);
    33.             newObject.transform.parent = go.transform.parent;
    34.             newObject.transform.localPosition = go.transform.localPosition;
    35.             newObject.transform.localRotation = go.transform.localRotation;
    36.             newObject.transform.localScale = go.transform.localScale;
    37.  
    38.             DestroyImmediate(go);
    39.         }
    40.     }
    41.  
    42. }


  15. Posts
    1,168
    Quote Originally Posted by morphus1 View Post
    Heya guys,
    I'm having a hard time trying to get this to work in my situation. The alt drag doesn't work for either. It says that this is not implemented yet. As for the script it says it can't find the prefab in the prefab library. any ideas?
    Same Here (3.5.2) - Alt-dragging a prefab from Project into a different prefab in Hierachy does not swap in the new prefab. It does nothing. (And occasionally crashes.) I don’t need the script and didn’t try it, but dragging manually would be great!
    Morgan
    adams
    immersive


  16. Location
    Germany
    Posts
    203
    I know this is an old thread but thanks ALOT, because this is saving me a lot of work (and nerves).


  17. Posts
    2
    im totally noob in this....
    how to apply this script on my prefab ?


  18. Posts
    1

    Noob here...

    Yeah, sorry guys, I also don't know how to make this work. I've created a new script (CopyComponents.cs), and the Custom > ReplaceGameObjects menu shows up...but when I try to use the option, I get a "NullReferenceException: Object reference not set to an instance of an object" error. What am I doing wrong?


  19. Posts
    4
    To the people that can't seem to get this to work. Make sure you scrips file name matches the class name.


  20. Posts
    7

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •