Use prefabs

    To instantiate a prefab, drag and drop it from the Asset View to the scene.

    You can re-arrange entities in the prefab instance just like you do with other entities:

    • create child and parent entities
    • drag entities to add them to the prefab instance
    • drag entities from the prefab instance to make them independent entities

    By default, Game Studio creates an empty parent entity with the prefab's entities as its children. The Entity Tree displays the prefab parent name in green next to the child entities.

    If you don't want to create a parent entity with the prefab, hold Alt when you drop the prefab into the scene. This is useful if you don't care about the relative positions of the prefab's entities and don't need to move them together as a group. For example, imagine you have a prefab composed of several crate entities arranged in a random fashion. It's not important that the crates maintain their relative position after you place them; in fact, several identical stacks of "randomly" arranged crates looks artificial.

    In this case, a parent entity is unnecessary. Instead, you can create several instances of the prefab, then re-arrange their individual crate entities to create the effect you need.

    | Relative positions maintained | Relative positions ignored|————————————————————————-|| Boxes duplicated |

    After you add a prefab instance, you can break the link between the prefab and any of its child entities. This means the child entity is no longer affected by changes you make to the prefab.

    Break link between child and prefab

    To use prefabs at runtime, you need to instantiate them and then add them to the scene in code.

    Note

    Just calling isn't enough to add a prefab instance to the scene. You also need to use . For example, if your prefab contains a model, the model is invisible until you add the prefab instance. Likewise, if your prefab contains a script, the script won't work until you add the prefab instance.

    If you have a prefab named MyBulletPrefab in the root folder of your project, you can instantiate and add it with the following code:

    Note