Unity Tutorial: How to Make and Instantiate Prefabs

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
one of the key building blocks of any unity app is prefabs prefabs allow you to store game objects for reuse without putting them in a scene this allows us to recreate elements that appear in multiple scenes or multiple times in a scene without having to rewrite our code repeatedly in this video i'm going to go over how to make a prefab how to instantiate a prefab and how you might use prefabs in a project to start with let's go over how to make a prefab this process is actually really simple first you just want to make a game object i'm going to make a simple cube here and then we can change its properties so let's make our cube a little bigger and let's add a script to it and what this script is going to do is make our object move across the screen when we hit play like so so now to make it a prefab all we have to do is click it and drag it into our project so now you can see it turned blue in our hierarchy and that indicates that it's a prefab so if we delete it here you can see it still exists in our project folder and you can see in here it still has the script we added and it has the changes we made to the scale so then if we want to add this to a scene we simply drag it back up into the hierarchy and if we hit play you'll see it still does the same thing now the advantage of prefabs is we can drag multiple into a scene or if we made a new scene we could add these to a new scene without having to completely remake our object another advantage of prefabs is that we can edit all three of our instances of this prefab and if we added it in other scenes all of those instances as well by editing our prefab files so let's say we wanted to make the cubes a little bigger we could change this to 3 3 and 3 and you can see the cubes have increased in size similarly we could edit our script that we attached to this object and it would apply to every instance of the prefab instead of having us reapply the script to each one the next major benefit of prefabs is that in addition to just dragging them into all the scenes that we need them in we can use code to instantiate our prefabs at runtime so let's take a look at our instantiate script here so we take a variable that we're going to put our prefab in we've called it cube here and then in start which means that this is just going to run when we hit play we run the instantiate function and that takes a few parameters first it takes the game object that we want to instantiate which in this case is going to be our cube which is our prefab and then we have to give it a position in this case we made a new vector three zero zero zero meaning that its position is going to be zero zero zero and then you also have to put in a quaternion property and all this does is determine the rotation of your object for our purposes identity is gonna work just fine so then we just have to attach the script to an object for the sake of demonstration i'm going to attach it to our main camera and we can drag our prefab into the cube variable spot so now when we hit play we should have a cube instantiated yep and it moves across the screen because it still has the script attached from our prefab so next i'm going to walk you through how you might use a prefab by showing you how i use them in a project of mine this is a work in progress card game that makes extensive use of prefabs and scriptable objects instead of having a prefab for every card in our game we save memory by having a single card prefab and then using scriptable objects to apply data to it and instantiating the cards we need at runtime so to start with let's look at our card prefab and drag that into the game here and you can see that basically our card prefab is just a bunch of ui properties with a script attached that applies data to it and that's what's going to fill in the art text and card name so the next thing that we're going to want to look at after our prefab is our card scriptable objects so these are passed into the script attached to our card prefab to apply data to our cards so you can see it takes a few variables including card name description and art and these are the things that will ultimately be applied to our current prefab which will make it look all pretty for the player so now that we know what the prefab and the scriptable objects look like we can hop into our script and see how the objects are instantiated so there's a lot going on in this script but there's only a few things that we need to talk about so the first is this variable card template and this is what our prefab is attached to so if i scroll down to our setup battle function which runs at the start of the game we can see we instantiate that here in this for loop that loops through all of the cards that we need to instantiate we start by creating a new game object and instantiating onto it our card template with a vector three of zero zero zero and quaternion identity just like we did with the cube earlier then we drill down into our prefab to get its component which is the script that we need to apply data to and we add a card to it which is the data that's going to make it appear then down here we set its transform equal to the area where we want it to appear that's just setting where it is and then we can change that later so that we can move the cards around as needed so the core of what this function does is just instantiate all of our cards with the information they need using a single prefab instead of 100 prefabs for 100 cards so now we can look at what our script actually does and if you look in our hierarchy now you can see there's no cards in it but if we hit play we go to deck area where the cards are instantiated you can see there's now several cards and we similarly use a process of instantiation to create these player characters and this enemy and then when we finish once again there's no cards in the hierarchy some advantages of implementing our cards in this way is that it saves us a lot of time and makes our scripts more flexible because i can change the appearance of every card we make by simply editing our card prefab because every card is instantiated using that prefab plus by using prefabs and scriptable objects we can instantiate thousands of cards at runtime which is much easier than the tedious process of dragging all of the cards into every scene ourself if you liked this video and you want to see more content like it consider hitting the subscribe button so that you never miss an upload and if you're considering using unity but you're on the fence consider checking out some verified user reviews of the software on thrustradius.com all reviews on trust radius are from real users who share their thoughts on software based on their use case and experience
Info
Channel: TrustRadius
Views: 34,141
Rating: undefined out of 5
Keywords:
Id: IfcCXVXjLNM
Channel Id: undefined
Length: 7min 14sec (434 seconds)
Published: Fri May 07 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.