![]() However, being able to instantiate Prefabs using code provides you with powerful abilities to dynamically create complex configurations of GameObjects while your game or app is running, as shown in the following examples. You can drag a different Prefab into the My Prefab field in the Inspector to change which Prefab is instantiated, without having to change the script.īecause this first example is very simple, it may not seem to provide any advantage over just placing a Prefab into the Scene yourself. When you start Play Mode, you should see your Prefab instantiate at position (0, 0, 0) in the Scene. ![]() Instantiate(myPrefab, new Vector3(0, 0, 0), Quaternion.identity) Ĭreate a new C# script in your Project, and name it “InstantiationExample”.Ĭopy and paste in the script example above into your new script, and save it.Ĭreate an empty GameObject using the menu GameObject > Create Empty.Īdd the script to the new GameObject as a component by dragging it onto the empty GameObject.Ĭreate any Prefab, and drag it from the Project window A window that shows the contents of your Assets folder (Project tab) More info See in Glossary into the My Prefab field in the script component.ĭragging a Prefab from the Project window into the My Prefab field in the script component Instantiate at position (0, 0, 0) and zero rotation. This script will simply instantiate the Prefab when the game starts. Drag a Prefab into this field in the Inspector. Public class InstantiationExample : MonoBehaviour It creates an instance of that Prefab in the Start() method. The script example below has a single public variable, “myPrefab”, that is a reference to a Prefab. You can then assign the actual Prefab you want to use in the Inspector. The public variable in your code appears as an assignable field in the Inspector. You can make this reference by creating a public variable in your code to hold the Prefab reference. To instantiate a Prefab at run time, your code needs a reference to that Prefab. ![]() InstantiatingPrefabsExamples.zip Basics of instantiating a Prefab Note: You can download a Unity Project containing all the examples on this page, here: You can make a simple rocket into a super-charged rocket, without any code changes. More info See in Glossary.Ĭhange which Prefab is instantiated without changing the code. More info See in Glossary view, Hierarchy and Inspector A Unity window that displays information about the currently selected GameObject, asset or project settings, allowing you to inspect and edit the values. In each Scene, you place your environments, obstacles, and decorations, essentially designing and building your game in pieces. Think of each unique Scene file as a unique level. Set up, test, and modify the Prefab quickly and easily using the Scene A Scene contains the environments and menus of your game. Creating equivalent GameObjects from scratch requires many more lines of code. Instantiate a Prefab using one line of code. Compared with creating GameObjects from scratch using code, instantiating Prefabs using code has many advantages because you can: More info See in Glossary or collections of GameObjects at run time. A GameObject’s functionality is defined by the Components attached to it. More info See in Glossary come in very handy when you want to instantiate complicated GameObjects The fundamental object in Unity scenes, which can represent characters, props, scenery, cameras, waypoints, and more. The prefab acts as a template from which you can create new object instances in the scene. Prefabs An asset type that allows you to store a GameObject complete with components and properties.
0 Comments
Leave a Reply. |