Search Unity

is there a performance difference using multiple prefabs v cloned prefabs

Discussion in 'Scripting' started by melonhead, May 25, 2017.

  1. melonhead

    melonhead

    Joined:
    Jun 3, 2014
    Posts:
    630
    as the title says, i keep seeing videos and hearing that you should only instantiate clones of prefabs and not prefabs, so what i want to know is, why, whats the difference in performance if any?


    thank you
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    I think you're confused about what Instantiate does, and what prefabs are.

    A prefab is just a predefined hierarchy of objects that is kept as an asset in your project. When you drag a prefab into the scene, Unity automatically Instantiates a clone of the prefab into the scene. In the same way, when you call Instantiate via code, it creates a clone of the prefab into the scene.

    So forgive me if I'm misunderstanding you, but your question doesn't really make sense because whether you're Instantiating a prefab, or a clone of a prefab, the result is the same. Instantiate literally means "to make an instance of", aka "to make a clone of"
     
    LaneFox likes this.
  3. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    I doubt those vids are giving performance advice, but simply explaining what is happening. When you instantiate a prefab, a clone of the prefab is put into the scene. Heck you can instantiate an existing gameObject in the scene to create a clone of it in the scene.
     
  4. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,521
    So many people get confused about this when they first start.

    You have at your disposal two separate Hierarchies. A) The Project Hierarchy and B) The Scene Hierarchy.

    The Project Hierarchy is effectively a library of Assets, or Prefabs.
    The Scene Hierarchy is effectively Clones, Copies, Instantiations, or References of something in the Project Hierarchy.

    The Scene does not contain prefabs, it contains either unique objects with no reference to the Project (eg they only exist in this exact scene) or cloned references (aka instantiations/copies) of something from the Project Hierarchy.

    So if you want something to be added to the scene at runtime you would call Object.Instantiate(aReferenceToMyPrefab) and it would literally go get that prefab, duplicate it, and put it into the scene. The Prefab itself remains in the Project Hierarchy untouched, simply a reference, and the new copy now exists in the scene as a completely separate object.

    Now ScriptableObjects are different, but you don't need to know about those yet.
     
  5. melonhead

    melonhead

    Joined:
    Jun 3, 2014
    Posts:
    630
  6. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065