Learn C# Scripting for Unity in 15 Minutes (2025 Working)
Introduction to C# Scripting in Unity
In this video, Raja from Charger Games introduces viewers to C# scripting in Unity. He covers a range of topics including destroying objects, using mouse clicks and keyboard inputs, and working with physics properties.
Creating a Test Script
- To create a new C# script, right-click on the scripts folder and select "Create New C# Script."
- Attach the test script to an object by dragging and dropping it onto the object.
Destroying Objects
- The
destroyfunction can be used to destroy game objects. Call it from anywhere in your code.
- To destroy an object after a set amount of time, add a comma followed by the time (in seconds) you want to wait before destroying it.
- To destroy an object when clicked on with the mouse, create a new function called
void OnMouseDown(). Comment out previous code and copy-pastedestroyfunction into this new function.
Using Keyboard Inputs
- Use the
Inputclass to take keyboard inputs.
- Use
if (Input.GetKeyDown(KeyCode.Space))to check if spacebar is pressed.
- Copy-paste
destroyfunction into this if statement so that pressing spacebar destroys the object.
Working with Physics Properties
- Add a rigidbody component to an object through the inspector window.
- Access rigidbody component through public variable in script.
- Use
RB.AddForce()method in update loop instead of destroy method to apply force on gameobject when spacebar is pressed.
Adding Force and Velocity to a Cube
In this section, the instructor demonstrates how to add force and velocity to a cube in Unity.
Adding Force to a Cube
- Pressing the space button adds force to the cube.
- The cube jumps as soon as the space button is pressed.
Adding Velocity to a Cube
- Comment out the code for adding force.
- Insert an update function.
- Add velocity in the forward direction using
RB.velocity = Vector3.forward * 20f.
- Freeze rotation on x-direction of rigidbody constraints.
- The cube moves in the forward direction without rotating.
Detecting Collisions with Other Game Objects
In this section, the instructor explains how to detect collisions with other game objects in Unity.
OnCollisionEnter Function
- Use
void OnCollisionEnter(Collision collision)function to detect collisions.
- Check if collided object has an enemy tag attached using
if (collision.gameObject.tag == "enemy").
- Destroy own game object if collided object has an enemy tag attached.
- Create a sphere with gravity and sphere collider.
- Add enemy tag from tags menu.
- Test collision detection by running simulation.
Loading New Scenes or Levels in Unity
In this section, the instructor demonstrates how to load new scenes or levels in Unity.
Creating a New Scene
- Go to File > New Scene and save it inside Scenes folder as Level 2.
Reloading Next Level
- Use
using UnityEngine.SceneManagementto import scene management functions.
- Use
SceneManager.LoadScene("Level 2")to load the next level.
- Press R key to reload the next level.
Loading Levels and Displaying Text
In this section, we learn how to load levels and display text on the screen.
Loading Levels
- Drag and drop the level 2 scene into the current scene.
- Press play to destroy the queue and load the new levels.
Displaying Text
- Create a new UI text element in Unity.
- Reset its position at the center.
- Change the text to "You Win".
- Adjust font size, color, and position.
- Name it "Win Text".
- Create a public variable for Win Text in C# script.
- Deactivate Win Text before starting the game.
- Activate Win Text when Spirit collides with Cube.
Checking Collisions and Moving Objects
In this section, we learn how to check collisions between objects and move them using arrow keys.
Checking Collisions
- Create a public variable for Win Text in C# script.
- Destroy Sphere when it collides with Cube.
- Activate Win Text when Sphere collides with Cube.
Moving Objects
- Create two variables for X input and Z input in C# script.
- Create a public variable for speed in C# script.
- Get values from horizontal axis (left/right arrow keys) and vertical axis (up/down arrow keys).
- Add force to Cube object in X direction using X input value multiplied by speed value.
- Freeze rotation on x,y,z axes to prevent unwanted rotation of cube object.
- Adjust speed value as needed.
Conclusion
In this tutorial, we learned how to load levels, display text on screen, check collisions between objects, and move objects using arrow keys.
Turn any video into a summary like this
YouTube links, meetings, lectures — with transcripts, search, and chat.