How to Spawn Objects anywhere in Unity3D (and a bit of addressables)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's up I'm Jason and when I first started using unity the first thing I wanted to do was spawn some objects and make them go flying around in fact I think it was a cannon and a cannonball I wanted to spawn some cannons and spawn some cannonballs and make those cannonballs fly out and go shooting across the world and I did it took me a couple hours to figure it out and get it kind of working but it was a lot of fun and I realized along the way that there were a lot of different ways to do this there were quite a few different ways to create a cannon ball I think I started by creating a primitive and adding a rigidbody and launching I don't even remember there were a lot of different options though and it did take me a while to figure out how to do it right so in this video I want to show you a couple different ways that you can spawn a game object I want to talk briefly about some things that you should watch out for wind spawning game objects other stuff to look into and then we'll also touch a little bit on the new addressable system which is a cool new way to spawn assets that is useful for more advanced stuff if you're into the beginner stuff that's what we'll hit first and then if you're into something more advanced just hang around and we'll get into addressable is a little bit near the end now if you're interested in this kind of stuff make sure that you share this video don't like it or subscribe it or any of that stuff just uh just share it okay do you like and subscribe if you want to but sharing is the most important thing also uh special thanks to everybody on patreon and everybody who emails me guys are all awesome let's get going so what I've got set up here is a basic little scene with a cube for a floor and then three different rocket spawners we had this one enabled at first we're gonna start with the simplest little rocket spawner here this is a class or eight well it's a game object with a single script that is a class that has a reference to a rocket prefab so I want to look at the prefab and then look at the code and show you exactly how this works and how we do all this so the first thing I'll do is just click over here on the rocket prefab and go take a peek at it it's in my prefabs folder and it's just this little mini rocket object it's got a rocket script on it it has a box Collider and a rigidbody in to transform and you can see a little preview of it by the way if you're not used to this UI if it looks a little bit different this is the beta version of unity 2019 point three the UI has changed a little bit it looks and feels a little different took me a while to get used to it but I like it now so let's take a look at this prefab in a little bit more details I'm just gonna double click on it to go to prefab edit mode and here you can see that we've got this atom rocket that I actually grabbed from the asset store again if you search in here like for rocket and then go to asset store you can see a whole bunch of really cool free options and I just did that grabbed it was that one right there thanks whoever made that awesome all these free packages so we've got an atom rocket model on a rocket and that's about it not much to it let's look at the code now and see how we actually do the spawning of this object so normally you know if you've never spawned anything the first thing you do is you drop it out into your scene go to your scene view and hit f2 go to it and then you move it around get it in place not it but we don't want to do that we want to do the runtime spawning so we're gonna do that all with the code in our rocket spawner to do that I'm gonna go to my rocket spawner and I'm just gonna double click on the script here well single clicked it popped it up down there but I'll double click it and open it up in my code editor I'm also using a slightly non-standard code editor I'm using writer just because I like it a lot more than the alternatives I've switched about a year ago and I wouldn't go back it's not free though but if you're interested go check out writer it's pretty cool as a free trial so here's our code we have a public class rocket spawner that's a monobehaviour so we can have it as a component on our game object that's what makes it so that we can add it right there and then we have two methods here we have an update method and then down here you'll see we have a spawn rocket at position method so the spot rocket at position is actually doing the work of spawning the object here and here we're doing it in a very simple way we're calling instantiate this is the same as game object instantiate we could do it either way but since we're in a monobehaviour we don't necessarily need the game object part that's all only reason it's different there then we give it this rocket prefab as the first parameter so we're calling instantiate with this prefab and then we're passing in a position this vector three spawn position and then quaternion dot identity which is just a default rotation so let's take a kind of pull back and look at this well what's calling rocket spawn rocket at position and how is that working and that's in our update so in our update method we're checking to see if the user has pushed the mouse button zero which is just our left-click so if they have pressed left-click we get the position where the mouse is on the screen so this input Mouse position is just a vector with the screen mouse position we don't necessarily have to throw it into a vector three I'm just doing that to make it a little bit more obvious what's going on on line 11 we're taking that value and we're putting it into this screen point to Ray method which is just a method on cameras and we're using the main camera I put a note here for anybody who's curious yeah you do want to cache your camera you don't want to use camera dot mean it's slow it uses tags it's there are a whole bunch of videos and reasons why you don't use camera dot mean but in demos it's totally fine because we're only gonna have one camera so we call screen point to Ray and this gives us a ray which is just pointing into the world it's like a invisible wine pointing into the world telling us the first thing that it hits and it tells us the first thing it hits when we call physics dot ray cast so really the ray is and at this point in a direction so we have the origin in the direction and then the ray cast does the thing hey does this ray go through anything and if it does put some info about that thing into this ray cast hit hit info object and one of the important things to put there is the point where that line intersected so when I'm clicking on that cube on the ground we're getting the point where that line intersects through the cue but right here and then we're passing that point in to instantiate spawning our rocket at position with that default rotation and that's all we need to spawn this object well almost all we need because I kind of glossed over this whole rocket prefab thing so on line 5 we have a private game object field that has this serialized field attribute in front of it serialized field attribute by the way makes it so that private fields can show up in the inspector and they can be edited in the inspector the alternative is to make this a public field so you just replace the word private with public get rid of the serialize field and it will show up there but it does break a little bit of encapsulation and it can cause problems long term in bigger projects where you accidentally misuse things so to keep things a little safer I always go at the private serialize field unless I need to access this prefab from outside the class here we don't so big rant about that but the main thing is that this is a reference to the prefab that we want to spawn and if we go back into unity take a look at our rocket spawner and see that we have that reference right there and let me click on it one more time well I need to have my project you open click on it you'll see that we go down and we get that selected so that's the very basic rocket spawner let's play and just see what that looks like I'm gonna hit play in here go to the game view should pop right up and I click and my Rockets up here so I click and some Rockets spawn cool those Rockets actually shouldn't be taking off so I'm not sure why they are the typed rocket spawner is supposed to make them take off and we're gonna actually just look at that and yeah why edit let's just go through and take a look and look at the typed rocket spawner I'll show you what the differences are and why the other one was not supposed to launch but probably did so here we go we'll uncheck the rocket spawner and we're gonna enable this typed rocket spawner I'm gonna save my scene ctrl s and then I'm gonna open up the typed rocket spawner script and I want you to take a look at this it's very very similar in fact if I go back and forth between the two other than my zooming difference you see the only real difference is right here and right here what we're doing is we're defining the type of component that's required for this game object reference so now in our types rocket spawner we're actually requiring that our prefab be a rocket if we look back at our other one this rocket is wandering it just has to be a game object we could technically throw anything in there I don't want to grab those but like I could throw this random directional light in there and that would be spawning copies of this random directional light in fact let's do it watch let's uncheck typed rocket spawner check type or rocket spawner which is really a bad name for it because it's not really what it's doing but in this example is what it was intended to do and then click and yep just spawning some whites if I uncheck maximise on play and pause you can see that we actually have all of our light show oh and I can hit pause again and you can see the lights just appearing so the difference there with that typed one is that we can't do that we can't give it this bad data so let's uncheck that one will re-enable the type one and if we try to drag in a directional and you see that that's not an option and if I set this to none see that it actually says it needs to be a rocket and there's none in my scene so I got to go back to my project for you go to my prefabs and drop that thing back in so this works the same except that when we do the instantiate we're getting back that rocket object instead of the game object now we could with the other one do a get component call on it and get the rocket component and do something with it but generally if we know the type of the thing we want to enforce that so that the wrong thing isn't put in there and that we don't have to make these extra get component calls so that is generally I'd say how you want to spawn an object at runtime now there are a couple caveats a couple very important things to mention and then I want to dive into addressable as really briefly and the most important one is that if you're spawning objects it can be a somewhat heavy process so depending on the platform in the scenario you may want to pre spawn all of these objects disable them and then allow them to be enabled and we usually do that with a pooling system I won't dive into pooling here I have a couple videos about pooling and their ton of other resources out there about pooling but if you figure out how to use a pooling system it's relatively simple you'll do the same kind of thing except you'd call like Poole dot get rocket or rocket pullback get or something and you get a rocket back out and it would handle all of that stuff and don't worry pulling may sound complicated it's not it's very simple and once you do it a couple times it becomes very easy too easy pattern to kind of throw in a lot of places and get big performance increase the other thing is destroying game objects is also pretty hefty so throwing them into a pool helps again now let's just dive into addressable scuzz I know some of you are out there thinking like hey all of this is very basic I just want to get to the addressable stuff how do we spawn with addressable x' and when i first started looking at addressable so it's thinking okay they're gonna be kind of like asset bundles kind of a pain a lot of work that you have to do to get everything set up and working so so we'll figure it out and then we'll do it but i doand I was actually really pleasantly surprised they're amazingly simple to use so I'm gonna stop playing and hit the first important part and that's that you need to go to the package manager and you need to import addressable so if you go in here and go for address you see the addressable SPAC äj-- now this eventually will probably just become part of the editor I don't know or maybe maybe it won't it will always be a package but for now you definitely need to go grab the address post package import that and then open up window and then asset management and addressable x' and it'll pop up asking you to create an initial configuration just hit that button and then your thing should look kind of like this your addressable window should look like this once you have that you go over to the mini rocket and I've checked the addressable box this box appears once I've set this this up and now I have this reference or this string path to where this addressable would be so if I want a reference this addressable by the actual address this is it it's assets prefabs mini rocket doc prefab and I can change this but I left Earth alone we can also do some tags and there's quite a few wait there's labels and all kinds of stuff or not tax I meant labels with labels and other stuff that we can do I'm not going to dive into those I'm just gonna go into the very basics of how to spawn these things so with this address so you might think like hey I'm gonna need to go in there now and put in a string reference in the code and things are gonna get messy but you can see if I click on well let's turn off the typed rocket spawner let's click on the addressable one if you look here you'll see that the rocket prefab thing it looks similar but it's actually a little bit different it's a drop-down and if you look here you'll see that it's showing me my available addresses so I've got this mini rocket let's go in here and make a mini rocket - so we'll make a mini rocket give it a number - and we'll give this a new address and then we should see the new address show up in there as well if I can rename things properly so we've got mini rocket - we make that addressable go into my addressable rocket spawner and I see that we have option 2 in there so it's as simple as that for setting it up but you might wonder well what the hell is this what's this rocket prefab how did you do that let's open it up take a look it's this asset reference so instead of putting the rocket or the prefab I put the asset reference and then I can select it from that list of addressable x' and in these addressable xym can be loaded at runtime they can be synced offline so if we want to pull these things down more like a resource bundle or asset bundle we can do that so if you're not sure what to use not sure if you should use addressable or if you should just use instantiate use instantiate if you have an issue where you need to load things at runtime you need to pull them down you need to better manage what things are loaded what things are unloaded as you go from level to level or as things load in and out of your level then asset bundles or I guess addressable is now are definitely worth looking into there's a whole lot of info about them and I'll probably do a full video just diving deep into addressable x' and how to use them in a bigger project so if you're interested in that make sure that you just drop a comment below and let me know if there's a lot of them I'll do it sooner than later but again if you're just doing more beginner spawning stuff learn more about the pooling don't worry about addressable yet it's much more of an advanced concept and more of a thing that you need in bigger projects it's helpful kind of neat but you may notice here that we're doing this instantiate a sink we don't actually have a reference back to the rocket we don't even have a reference back to the game object we need to register for an event we'll get that event callback when the game object is created and then we have to do the get component so there's still a good amount of work and it's not worth switching to addressable if you're not doing it to solve a problem if you're doing it to solve a problem great it seems like it's gonna be amazing for solving a lot of problems but if you don't have one of those problems then you're just over complicating things yourself so thanks for watching if you're interested in this stuff again don't forget to share it don't like don't subscribe it just share it don't put it up on Facebook or MySpace or Instagram or whatever other thing you can think of and anyway thanks for watching also special thanks to everybody on patreon you guys are awesome we're doing lots of meetups and stuff so if you're interested in that stuff go check it out and bye you
Info
Channel: Jason Weimann
Views: 25,351
Rating: undefined out of 5
Keywords: instantiate, gameobject, gameobject.instantiate, unity, unity3d, spawn object unity 3d, spawn object unity 2d, unity tutorial, unity 3d, unity tutorial for beginners, game development, beginner, game dev, make a game, unity simple game tutorial, how to make a game, tutorial, c#, game, tutorials, instantiation, unity3d college, unity instantiate, unity spawn, unity tutorial for beginners 2019, game development college, addressables, addressable assets, unity tutorials
Id: Zb9WchxZhvM
Channel Id: undefined
Length: 17min 0sec (1020 seconds)
Published: Tue Sep 24 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.