Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[0b4] Editor: PrefabUtility.InstantiatePrefab from a scene prefab how to? Or broken?

Discussion in 'Prefabs' started by Quatum1000, Oct 7, 2018.

  1. Quatum1000

    Quatum1000

    Joined:
    Oct 5, 2014
    Posts:
    889
    Hi,

    I'd like to clone a prefab that is already in the scene as a prefab.

    Code (CSharp):
    1. var go = PrefabUtility.InstantiatePrefab(scene_prefab) as GameObject;
    That produce a null gameobject. I tried gethandleof... and others, but that does not work anyway.
    I'm doing anything wrong? Never had this issue.

    Thank you..
     
    forestrf likes this.
  2. forestrf

    forestrf

    Joined:
    Aug 28, 2010
    Posts:
    223
    Last edited: Jan 12, 2024
  3. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,294
    Edit: Oh, a necro-post.

    I think you're looking for PrefabUtility.GetPrefabObject().

    Code (CSharp):
    1. GameObject prefabAsset = PrefabUtility.GetPrefabObject(scene_object_which_may_be_part_of_prefab_instance);
    2.  
    3. GameObject instance;
    4. if (prefabAsset != null)
    5.     instance = PrefabUtility.InstantiatePrefab(prefabAsset);
    6. else
    7.     instance = Instantiate(scene_object_which_may_be_part_of_prefab_instance);