Monster shadow


MonoBehaviour? Let me teach you my guy [Unity Tutorial]

Learn what a MonoBehaviour is, how to create and user them and what it gives you access to. Also, learn about lifecycle hooks and drawing gizmos to the scene. ❤️ Become a Tarobro on Patreon: ========= 🔔 SUBSCRIBE: 🗨️ DISCORD: ✅ MORE TUTORIALS: About Tarodev: Develop video games like a pro! Whether you're a beginner or a seasoned game developer, you'll find lots of useful tips and tricks to boost your development career. Visit the channel: MonoBehaviour Documentation:

Great video. I love how short and clear these are. I've been using Unity for years and I am a bit ashamed to admit that I have never really understood monobehaviour until now. Thank you very much. I think that the Unity development team should use you for their how to videos.
Is it possible to access the GameObject the MonoBehavior is attached to? Let me give an explicit example to clarify my question: I have a Hitpoints class attached to a GameObject and a Skills class attached to the same GameObject. I need to access the Constitution.Level from the Skills object so that the Hitpoints object can calculate the MaximumHitpoints.
You sure can. From your hitpoints script you can grab your skills script like so: var skills = GetComponent<YOURSKILLSSCRIOTNANE>(). This will search the current game object the script is attached to.
Thanks for the video! I would say that you speak a bit slow; maybe a bit of editing can solve that, but other than that, the information was on point and overall a good video! Keep Going!
Taylor Freeman
That's also why YouTube allows speed adjustment so each viewer can watch at their desired speed.
Thanks elain. Good feedback as I also enjoy nice quick tutorials. I'll keep it in mind for the future!
Good vidio :)


a monobehaviour is a base class in c-sharp which the unity engine
provides and it allows you to attach your scripts
to game objects so to create a new monobehaviour
simply right-click create c-sharp script and name it what you like
so as you can see shape is inheriting from
mono behavior and at its base functionality
what this will allow you to do is it will allow me to
grab my shape script and put it directly on my object
as you can see there so mono behavior is actually the only way
you can attach a script to an object so what what does it provide first up as
you'll see it gives you access to these lifestyle
hooks so for example start will run just once on
your script initialization so you'd put
things here that uh you only want to happen once for
example um setting some text or setting a health
bar or things like that update is called every
single frame so here you could be looking for for
example uh a button press
and then if that if if it detects the button press then you could say
to move forward or to rotate or what have you
you can also directly reference the object that this script is attached
to so you could write transform and that there
is actually pointing to our shapes transform
right here so we can directly grab it and then we can manipulate it so if we
put this here in update we could go transform rotate and then we could do
like a vector3
left times time that delta time and that would just be a super simple
uh well it is rotating i i made it go extremely slow but there we
go so that this script is directly
referencing the transform that it's attached to and it's rotating the shape
like that uh you can also grab references to other components so let's
say we wanted to grab this mesh renderer here
we could go mesh equals a get component
and mesh renderer so that will actually search the game object that it's on and
it will grab this reference and now we should be able to manipulate things like
the material like that and we could change it to a
new material or whatever you want to do it also gives you access to a whole load
of other life cycle events like for example awake
which is called just once as well but it's called before start
so i usually use awake methods for my managers so like if i'm creating a
single instance of this class and i want it to be like for
example a sound manager if in start i want to call a sound every
single time that this shape spawns i would need to i would need to make
sure that my sound manager has already been initialized
so in my sound manager i would be using this awake function and then
here in this object i would use start and then reference my sound manager
it also gives you access to collisions like uh whoops on collision enter
and on trigger enter and all that so when this shape collides with another
collider it will it will call this function and then you
can you know perform your logic explode or take damage or what have you
another thing that you can do with a with a monobehaviour is you can
serialize variables uh so that you can see them in the editor
so let's actually go back to our rotate function here
just write this out again times time dot delta time now
instead of just hard coding in the 10 there i could say
public load speed and i could put that in there
and now on our shape we'll see we've got this speed parameter here
so we can just put that up and then we can also edit it in runtime
you could go faster now one word of warning though exposing
variables as public just so that you can see them in the
editor is in my opinion bad practice because not only does this expose it in
the editor it exposes it in other scripts
so if if if another object or another class
got a reference to this shape i would be able to actually see
this speed variable here so a way that we can fix that is we can make it
private which by the way will prevent it from
being serialized i'll show you we now can't say it in the editor but
you can add an attribute called serialized field
and let's just change this to be proper c sharp private convention
naming conventions and now it's back
just like that and conversely if you did have a public variable of some kind
let's say you've got this and your it's your direction your
current direction now because this is public
we will be able to see this in the editor but
this may not be something that you want to serialize in the editor
so you could give it the attribute hide in inspector
and now this is this is accessible from other classes
but it will it won't clutter up our inspector here
so just some more monobehaviour goodies so
another thing that it gives you is on draw gizmos
and let's just set our gizmo color to red just so we can see it now we can go
gizmos dot draw wire sphere and
the position of the wire spear swiss sphere sorry
let's say uh transform position plus vector three right
times three so basically our position that this script is attached to
and then three units to the right and then we'll say the radius of this shape
is two so now on draw gizmos is called consistently while you're in the editor
so you'll see now
i've got a wire sphere that will always be
three units to the right of my shape you can't see it in the game view you can
actually do that um yeah just turn on gizmos in the
in the in the view but this is uh incredibly handy for
for uh debugging and level editing and all that kind of stuff so that's about
the scope of this video uh there's a lot to learn about the mono
behavior um and what it what it offers you so if you want to
learn more about it i'll leave a link in the description
if you like the video or i taught you something give it a like
subscribe and i'll see you next time