Learn C# Scripting for Unity in 15 Minutes (2024 Working)
Summary
TLDRIn this Unity tutorial by Raja from Charger Games, viewers are guided through basic C# scripting techniques in Unity within 10-15 minutes. The video covers essential topics such as object destruction, keyboard and mouse input handling, physics integration with rigidbodies, collision detection, and loading new scenes. Additionally, it demonstrates how to display text on screen and control game objects using arrow keys. The tutorial is fast-paced, offering practical, step-by-step instructions to help users quickly grasp Unity scripting fundamentals.
Takeaways
- 🚀 The video focuses on learning C# scripting in Unity within 10-15 minutes, using a 3D cube as the example.
- 📦 Start by creating a C# script, attaching it to the cube, and learning the basics of the Start and Update functions.
- 💥 The Destroy function can be used to remove the game object immediately or after a delay, such as 3 seconds.
- 🖱️ The OnMouseDown function allows you to destroy the object when clicked with the mouse.
- ⌨️ You can also use keyboard inputs, such as pressing the spacebar, to destroy the object using the Input.GetKeyDown function.
- 🏀 Physics in Unity is demonstrated by adding a Rigidbody component to the cube, allowing it to react to gravity.
- 💨 Rigidbody forces, like AddForce, can make the cube jump or move by pressing keys, adding velocity in specific directions.
- 🔄 Collisions with other objects can be detected using the OnCollisionEnter function, which checks the object's tag and triggers actions like destroying an object.
- 🎮 Scene management is introduced, where pressing a key like 'R' loads a new level or scene in Unity using SceneManager.
- 📝 Text elements, such as displaying 'You Win' on collision events, are also covered using Unity's UI system and GameObject.SetActive.
Q & A
What is the purpose of the 'Start' and 'Update' functions in Unity scripts?
-The 'Start' function is called at the start of the game when the game begins, while the 'Update' function is called repeatedly every single frame.
How can you destroy a game object in Unity using a script?
-You can destroy a game object by calling the 'Destroy' function with the game object as its argument, typically within the 'Start' or 'Update' function.
What does the 'OnMouseDown' function do in Unity scripts?
-The 'OnMouseDown' function is called when the object with the script attached is clicked with the mouse.
How can you make a game object respond to keyboard inputs in Unity?
-You can use the 'Input.GetKeyDown' method to check if a specific key is pressed and then perform an action, such as destroying the object, in response.
What is a Rigidbody component and how does it affect a game object in Unity?
-A Rigidbody component makes a game object part of Unity's physics engine, allowing it to have mass and be affected by gravity and other forces.
How can you add force to a game object in Unity to make it move?
-You can add force to a game object by accessing its Rigidbody component and calling 'RB.AddForce' with a Vector3 direction and a force amount.
What does the 'OnCollisionEnter' function do in Unity scripts?
-The 'OnCollisionEnter' function is called automatically when the game object collides with another game object, allowing you to perform actions based on the collision.
How can you detect if a game object has collided with a specific tag in Unity?
-You can check the tag of the colliding object within the 'OnCollisionEnter' function using 'collision.gameObject.tag' and compare it to the desired tag.
What is the process to load a new scene in Unity when a certain condition is met?
-To load a new scene, you can use the 'SceneManager.LoadScene' function with the scene name as its argument after ensuring the scene is added to the build settings.
How can you display text on the screen in Unity using UI?
-You can create a UI Text element, position it, set the text properties, and then activate or deactivate it using script by setting the 'SetActive' property to true or false.
What is the role of the horizontal and vertical axis in controlling game object movement in Unity?
-The horizontal axis typically corresponds to left and right arrow keys, while the vertical axis corresponds to up and down arrow keys. You can use these axis values to control the movement of a game object.
Outlines
🎮 Introduction to C# Scripting in Unity
Raja introduces the video, explaining that viewers will learn C# scripting in Unity in a short span of 10-15 minutes. He quickly walks through setting up a basic Unity project, where a cube object is selected for scripting experiments. The script includes two main functions: `Start` and `Update`, where the former is executed at the start of the game, and the latter runs every frame.
🛠️ Object Destruction and Time Delays
Raja demonstrates how to destroy a game object in Unity using the `Destroy` function. He shows how to set an object to be destroyed immediately when the game starts or after a specified time delay (e.g., 3 seconds). The tutorial covers how to modify the script to destroy the object either at game start or based on user input, such as mouse clicks and keyboard presses.
🚀 Applying Physics and Adding Forces
Raja explores Unity's physics engine, focusing on adding physical properties like gravity to a cube object by attaching a Rigidbody component. He walks through scripting the cube to react to user inputs, such as pressing the spacebar to apply a force that makes the cube jump upward. He also explains how to add velocity to the cube to move it in a specific direction and introduces collision detection by attaching an enemy tag to objects.
🌐 Scene Management and UI Elements
Raja covers Unity's scene management system by showing how to load different scenes (or levels) based on user input, such as pressing the 'R' key. He explains how to use UnityEngine's `SceneManagement` class to switch between scenes and demonstrates adding UI elements, like a win text that activates when certain conditions are met, such as collision detection between objects.
Mindmap
Keywords
💡C# Script
💡Unity
💡GameObject
💡Destroy Function
💡Start and Update Functions
💡RigidBody
💡Input Class
💡AddForce
💡Collision Detection
💡Scene Management
Highlights
Introduction to Unity scripting in C# with a quick project setup.
Explaining the default 'Start' and 'Update' functions in Unity scripts.
Using the 'Destroy' function to remove objects in Unity, with a demonstration of immediate destruction upon game start.
Modifying the 'Destroy' function to delay object destruction after a specified number of seconds.
Implementing mouse click detection with the 'OnMouseDown' function to destroy an object when clicked.
Using keyboard input detection to destroy an object when the spacebar is pressed, using 'Input.GetKeyDown'.
Adding physics to the cube using a Rigidbody component, enabling gravity for realistic movement.
Adding an upward force to the cube when the spacebar is pressed, demonstrating basic physics manipulation with 'AddForce'.
Using Rigidbody velocity to move the cube in the forward direction continuously, with adjustments to prevent unwanted rotation.
Detecting collisions with the 'OnCollisionEnter' function, and destroying an object tagged as 'enemy' upon collision.
Demonstrating how to destroy the colliding object (a sphere) instead of the player’s object (the cube) upon collision.
Loading and switching between scenes with 'SceneManager.LoadScene' triggered by pressing the 'R' key.
Displaying a 'You Win' text element on the screen after a successful collision event using Unity's UI system.
Moving the cube left and right using arrow keys, adding force in the horizontal direction with 'AddForce' based on user input.
Wrapping up with suggestions for further learning and promoting additional Unity courses for deeper knowledge.
Transcripts
hello this is Raja from charger games
and welcome back to another new video so
in this video we're gonna learn about si
shop scripting in unity in just 10 to 15
minutes so this is gonna be fun let's do
this
alright so I'm gonna go really fast
because we have to cover a lot of things
in this short time so let's do this so
here I have a 3d project opened and I
have a cube right here and we're gonna
do all our experiments on this cube only
so here I have a scripts folder inside
that I'm gonna right-click and create a
new C sharp script I'm gonna simply name
this one test then I'm gonna go ahead
and select my cube and drag and drop the
test script on the cube then double
click to open it in Visual Studio so
here our script has been opened so first
things first we have these two functions
created by default the start function
and the update function the start
function gets called at the start of the
game when the game starts and the update
function gets called again and again and
again every single frame all right so
the first thing we're gonna do is we're
gonna learn to destroy the object so in
order to destroy the object we have a
function called destroy that you can
call from anywhere so we're gonna call
it from the start so we're gonna write
destroy and what are you gonna destroy
we can destroy this game object with
which the script is attached so you can
write game object exactly like this not
in capitals game object in small capital
like this so now we are calling it from
the start so it should get destroyed as
soon as the game starts so if I save the
script and go back to unity now let's
click on play and when the game starts
as you can see the cube cut' destroyed
all right so destroy function is working
so let's open the script again now we're
gonna learn to destroy it after a few
seconds so to do that we're gonna write
a comma and give the amount of time I
want to destroy it after so let's say
when we destroy up to three seconds I'm
going to write 3 F and if you want to
say float unit write F so we're gonna
destroy it after three seconds so in
unity we can play and then wait for 3
seconds 3 2 1 boom and as you can see in
3 seconds it get destroyed so that code
is working alright so destroy code is
working now let's learn how we can
destroy it by clicking on it to do that
here we're going to create a new
function we can
it void on Mouse down all right so this
function gets called when we click on
the object with our mouse alright so
here what I'm gonna do is we're gonna
comment this out by writing this double
slash that means this code is not
working anymore
and we're gonna simply copy this paste
it here and remove the 3f from here so
now whenever we will click our mouse on
the object or the cube it should get
destroyed so let's save the script by
pressing ctrl s and here let me go ahead
click on play now let me click on it and
as you can see as soon as I click on it
the cube gets destroyed click and it
goes destroyed so our mouse click is
working alright so now let's learn how
we can do that by using our keyboard
inputs so in order to use keyboard
inputs
first of all let's learn how to take
keyboard inputs to the keyboard inputs
we need to use the input class okay and
so we're gonna say if input dot get key
down key code dot space all right so
here we are pretty much saying that if
we press the space button on our
keyboard then what you want to do we
want to do this going to destroy the
object so let's copy it paste it right
here all right so now our update will
check every frame if we are pressing the
space button and then it will destroy
the game object now click on play and
three two one space and it got destroyed
so as soon as I press the space button
it gets destroyed now let's learn to use
some physics properties in the project
so here I'm gonna select the cube go to
add component and go to physics and from
here I'm gonna add a rigidbody component
so now it's part of unity physics engine
and as you can see it uses gravity so it
has gravity now so if I click on play
and move the cube upwards as you can see
it automatically falls down due to
gravity so now we can use this rigid
body and do anything we want with
unities physics engine and here we're
gonna create a new public variable not
public just rigidbody RB and inside
start begin a write our V equals get
component rigidbody so here we are
pretty much getting access to the
rigidbody that we
attached to our game object so now we
can do anything that we want just by
using this RB variable so in the update
instead of destroying it let's add a
force to our game object so here we
gonna comment it out and now we're gonna
write whenever we press the space button
we want to say RB dot and force and what
do you want to add force we want to add
force in the upwards direction so for
that we're gonna write vector three not
up and how much you wonder at fourth
let's say five hundred amount so we want
to add a force of 500 amount in the
upwards direction to our object whenever
we press the space button so let's save
this click on play and let's press the
space button and as you can see as soon
as I press the space but in our cube
jumps because we are adding a force to
it okay so now let's add a velocity to
our cube to move it in a particular
direction so in this case we can move
our cube in the forward direction to do
that what I'm gonna do is I'm gonna
comment this cut out and now here I'm
gonna insert the update function I'm
going to simply say RB dot velocity so
we are adding a velocity to a rigid body
and we're gonna add a velocity in the
forward direction so we're gonna say
vector three dot forward I multiply it
by let's say 100 F oh let's say 20 F so
we are adding a velocity of 20 in the
forward direction so we're gonna save
the script so now if I click on play you
will see how cube moves in the forward
direction by this amount and as you can
see it's already rotating so we don't
want it to rotate so we gonna come to
our cube come to the rigidbody
and go to the constraints and click on
freeze rotation on the x-direction all
right so now if I click on play you will
see our cube is not rotating anymore
okay so now let's learn to detect
collisions with other game objects in
order to detect collisions we have a
function void on collision enter ok and
now inside this collision variable we
have all the information about the
object with which we have collided so
here we gonna check if collision that
game object dot tag equals enemy so that
pretty much means if the object with
which we have collided has an enemy tag
attached then we can simply destroy our
game object so this
function gets called automatically
whenever our game object gets collided
with any other game object then it will
check if the game object has an enemy
tag attached then it will destroy our
own game object so let's save this and
go back to unity so here I'm gonna
create a new 3d object sphere and I'm
gonna put the sphere upwards then I'm
gonna add a rigidbody to it physics
rigidbody so now it has a gravity as
well and it has this sphere Collider as
well and now I'm gonna go to these tags
click on app tag click on plus and add
an enemy tag okay then again I'm gonna
come to the sphere select the enemy tag
from here so now it should detect
collision so now if I click on play the
sphere falls down it collides with the
cube and before that our cue moves so so
before testing this we need to comment
out this line of code so now things
should be fine so here we are back
inside unity I'm gonna click on Play the
sphere falls down onto the cube and as
you can see as soon as it falls down the
cube gets destroyed because the cube
gets collided with an object which has
the enemy tag attached so our collisions
are working so now instead of destroying
the cube we're gonna destroy the sphere
whenever it collides with the cube so
let's see how we can do that so here we
are back inside our script so here we
are checking if our object is colliding
with an object which has the enemy tag
attached then we are destroying our own
game object so now we need to modify the
code and instead of this let's copy this
and paste it right here and comment out
this line of code so now instead of
writing game object we're gonna write
collision daaad game object okay just by
writing this simple code instead about
cube now the sphere will get destroyed
now let's click on play and three two
one boom instead of destroying the cube
this time the sphere gets destroyed
because we have written the code in a
different way okay so now let's learn to
reload or load a new scene or new level
in our game so if you go to file bill
settings you can see currently we don't
have any scenes in the build so let's
click on add open scenes so our first
scene isn't the build we need to create
another new scene to learn this
now we're gonna go to a file new scene
then press ctrl s to save this and you
can save it inside the scenes folder and
we're gonna name this level 2 then click
on save
so now we have a new level now we're
gonna go to the scenes folder and go
back to our level go back to our game
level and we're gonna open our script so
here inside the script we're gonna say
if we press the R then we're gonna go
back to our next level okay to check
that here we gonna say if input dot get
key down key code dot R if we press the
Add button then you want to reload the
next level now in order to load the next
level here we need to import a new thing
or use a new name space we need to write
using unity engine dot scene management
ok so just by writing this code now we
will be able to use all the scene
management related functions so now here
when to write scene manager not load
scene and which scene we want to load
with in quotations when it ride level 2
and then a semicolon so now whenever we
press the our key our scene manager will
load the level 2 level in our scene so
let's save this and go back to unity now
this is the most important step you need
to go to file build settings and click
on add open scenes we have only once in
here let's go to our scenes and drag and
drop the level 2 and put it here
so both of these scenes should be here
before we can actually load them so now
if I click on play it goes down the
queue gets destroyed I'm gonna press R
and as soon as I press the are the new
levels got loaded so as you can see this
way we can load a new level just by
pressing a key now let's go ahead and
learn to display a text on the screen so
here I'm gonna go to UI and create a new
UI text element I'm gonna go to reset to
reset its position at the center then
I'm gonna go and change the text to you
win I'm gonna go to go and select the
direct selection tool and double-click
on the text then I'm gonna make it then
I'm gonna go to the 2d mode and make it
bigger something like this then I'm
gonna go and change the font size and
make it big
and then I'm gonna change the text color
to white put it on the center something
like this now we have our text let's
name it to win text okay now let's go
back to our script so here first of all
we need to create a public variable
public gameobject
win text and now whenever our Spirit
collides with the cube we want to we
want to actually show the Windex
so whenever our spirit collides with
cube first of all you're gonna destroy
the sphere and then I'm gonna say win
text not set active to true so before
starting the game we're gonna deactivate
the win text and when the game starts
and when it collides with the game
object we're gonna set it to true so
that it shows up on the screen so save
these and go back to unity in the
beginning we need to set the wind text
and disable it select our cube go to the
script as you can see it is waiting for
a reference to the wind text we can a
drag and drop our wind text right here
so now it has got access to it now we
can a click on play and as you can see
when it will come and destroy it our
wind text will be displayed so this way
we know how to check collisions and
whenever how to actually activate or
deactivate an object when some event
happens ok so the last thing we're gonna
learn is how to move our cube left and
right by pressing the left and right
arrow keys so here we gonna create two
new variables float X input float Z
input and we gonna create another
variable public float speed okay so now
inside the update we gonna say X input
equals input dot get axis horizontal and
Z input equals input dot you get access
vertical so we get a value from the
horizontal axis when we press the left
and right arrow keys we get a value from
the vertical axis when we press that up
and down arrow keys now here we gonna
say our B dot add force and
addforce in our three different
directions so in the x-direction we can
add a four sub X input fix input
multiplied speed in the y direction
we're not going to add any force so make
0 in the Z we're gonna add a 4 so we're
gonna say the input multiply speed
alright
so now just by writing this simple code
a force will be added in the X direction
and in the Z direction to our cube game
object whenever we press the left and
right arrow keys so now save this go
back to unity here we can select our
cube and as you can see it is waiting
for a speed value let's give the value
of 2 for now let's go to click on play
and if I press the left arrow key okay
so first of all if I press the left
arrow key our cube should move left so
let's go ahead and turn up the value of
the speed I'm gonna make it about
hundred let's say now if I click on play
you will see all these things happen now
if I click on left our key moves to the
left and it automatically rotates so in
order to stop that we're going to go to
our constraints and click on freeze
rotation on x y&z three of these now if
I click on play if i press left it moves
to the left if i press right it moves to
the right so now as you can see our cube
10 to the left and right and if you want
you can go ahead and adjust the speed
value I'm gonna make it 50 let's say now
if I click on play it should work fine
so now as you can see it gives a better
player controller
setting so now is as you can see I can
move our cube left and right just by
pressing the left and right arrow key on
our keyboard so thank you so much for
watching this video I hope you really
enjoyed by learning c-sharp unity
scripting in this short amount of time
so if you want to learn more you can
check out all my courses from the links
in the description below and check out
my other videos as well so thank you so
much for watching this is Raja from
charger games and I'm gonna see you in
the next videos
Ver Más Videos Relacionados
Learn C# Scripting for Unity 15 Minutes - Part 4 - Foreach Loop, Array, Find Tags
Learn C# Scripting For Unity in 15 Minutes - Part 2
Let’s Make an AR App in 15 MINUTES!! (Beginner Friendly)
Visual Scripting Crash Course | 3. Understanding Variables and Data Types
Godot 4 Crash Course #4 - Adding Meteors
25 - Reading Images, Splitting Channels, Resizing using openCV in Python
5.0 / 5 (0 votes)