Monster shadow

VIDEO

Prefabs and; Prefab Variants - Unity Tutorial

Description:
Learn what prefabs are, how to use them, and how to take advantage of prefab variants to maximize re-usability in your games.
ADD A COMMENT

Akash Tech Developer
Hi sir ji I need help how to access child of prefab through script in unity?
Akash Tech Developer
@Tarodev okk
Tarodev
Depends what you need to grab. You there is a script or component on a child you need you can do: transform.GetComponentOnChildren<THECOMPONENTYOUNEED>(); If you need simply one of the child transforms you can do transform.getChild(i)
Vishnu Jayaraj
Which shader you are using for slime?
Ryan
such a good lesson!
marilynjay
Super helpful! I understood prefabs but I didn't understand the variants until this video.
Tarodev
They're super handy. Always make a 'base' variant for game units as they'll usually share certain components like healthbars, colliders, etc.
İlyas Köse
Nice
srisrid multi purpose channel
good vidio will help when making longer games whe I am at intermidiat level in game dev. :)
SEE ALL COMMENTS


Transcript:

so let's learn about prefabs so in short a prefab is just a way to
take an object apply some changes to it like a material or in a script
and then save it so that you can reuse it
later in your scene so i'll show you how to do that so i've just got this
weird shape that i made in blender if you want to follow along you can use
whatever you'd like cube will do just fine let's add a material to it
like that and not too exciting at the moment
so let's add this little script that i made just called rotator
and we'll say we'll turn it on the x-axis at
20 speed so there you go now let's say we want to
use this across our scene 100 times and
better yet we want to use it across multiple scenes so
what we could do is we could simply just duplicate it like that and that'll work
but now what if you wanted to all of a sudden change the speed of all
of them uh all hundred across your scene and on
other scenes so it would be tedious to come in here
change this speed go to this one change this speed and do
it with all of them so that's where prefabs come in so let's
just delete those and now that we've got our object here
let's call it shape i'm going to create a new folder
prefabs um that didn't work again prefabs and let's just take our
shape and drag it into prefabs now that has
now created a prefab for us to use as you can see all the settings here to
use this prefab we can simply drag it and pull it onto
the scene and as you can see it's got all the
components from the original okay and you can also grab the prefab
that's already in the scene and you can simply duplicate it and that
will keep the prefab link that is that is there now that we've got all of
our prefabs here and say we've got 100 throughout our
scene you can simply edit in a few ways you can click on this
on any of the prefabs in the scene you can come down and let's say that
this is too fast so let's go back to 20. this now as you'll see is still only
effective on this one and that's because on prefabs you'll get
this little menu up here the prefab menu open select and
overrides and if you click overrides you'll see that i've made a change to
this script here so if i just apply all that will apply
the changes to the prefab so if i press play you can
see that it's taken effect across the board
another way to change prefabs is you can actually double click
to enter prefab mode another way to enter prefab mode is to just
click it on in the scene and click this button here open and that will actually
open it with the scene view present and you can make it normal
gray or hide everything else in the scene so
now that we're in prefab mode there's no need to
use this override menu as you can see it's hidden any changes while
in prefab mode will immediately be saved so
let's make this shape also rotate on the z-axis
and you leave prefab mode by just pressing this little button over here
so and now they're they've all taken effect
one other thing note in prefab mode is up here is a little menu for prefabs
so as you can see i've got autosave here if you turn this off if maybe you're
doing something a little bit delicate and you you don't
want to auto save and go over your previous changes
turn off auto save and you can change this
but then you'll have to manually press the save button
uh so i'll just leave auto save on i'll change it back to 20
and there we go so now the question is what if we would like to have a base
prefab but then we would like to have other
objects based off the base prefab with minor changes so i'll show you how
to do that so i'm just going to delete these ones
and from our prefab here right click create prefab variant
so that will create a brand new prefab and it will be
it will be based off this prefab so it will still have the link
to the original prefab and i'll show you what i mean so let's pull this
shape variant out to the scene and let's give it a new texture a new
material okay and then let's also change
this to this and let's spin it by 40. so i will
now when i override these changes it will actually be overriding it
to the shape variant and it won't override
the shape it's also good to note that now that we have over overridden
some settings here and also the material because we
are overriding it on the shape variant that means that when we change
those specific details on the original shape on the original prefab it won't
take effect on this new prefab because we have overridden those settings
so there you go let's do it again now to create another shape variant you
can do the exact same thing or you can just duplicate
another one and that will work just fine so this is a copy of
the original variant so let's give it a new
slime texture there and let's say we only want to
spin this on the zed and we'll spin it by 25. so i'll just do that one more
time we'll get out one more of them whoops i'll duplicate the
shape variant make another one give it another material maybe spin this
by 75 on all axis okay and we'll override this
on this one and we'll override the settings here that i changed on this one
so now we've got four one prefab and three variants
so here's where it gets interesting so we can go
to say the original prefab here let's open it up and because these are
all based off it if we make any other changes to this
to this base these will also be affected but let's do something weird here let's
create a little let's create a cube let's pull it off to
the side a little bit smaller and as you can see
it's already taking effect to all the other
prefabs there see that that's starting to look pretty
crazy and you can go to these variants
let's say let's go to uh variant 2 and we can actually override the base
settings so we could turn this off and then we could possibly even add a
sphere oops a sphere do some really cool things but all three
of these are linked to this one but can override
base stats and changing things on the base will also
affect the child the child variants as long as the child variants have no
overrides of the base stats that's just about all i have to say
about prefabs and prefab variants i hope you learned
something if you did give it a like subscribe and
i'll see you next time