Learn C# Scripting for Unity in 15 Minutes (2025 Working)

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 destroy function 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-paste destroy function into this new function.

Using Keyboard Inputs

  • Use the Input class to take keyboard inputs.
  • Use if (Input.GetKeyDown(KeyCode.Space)) to check if spacebar is pressed.
  • Copy-paste destroy function 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.SceneManagement to 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.

Video description

Get My Complete C# Course Here: http://bit.ly/31UgmOB ( 25+ Hours of Video Content ) |------------- ( Click On Show More ) ---------------- | Intermediate C# Scripting Course: https://bit.ly/3DhstLA Best Game Dev Courses: 1. Unity By Example: 20+ Mini Projects (20+ Hours): https://www.udemy.com/unity-game-development-by-example/?couponCode=YOUTUBE9 2. Build 5 Games in Unity & C# (21+ Hours) : https://www.udemy.com/unity-2d-game-development-by-example/?couponCode=YOUTUBE9 3. Complete C# Scripting for Unity (25+ Hours) : https://www.udemy.com/unity-c-sharp-scripting/?couponCode=YOUTUBE9 4. Unity Android Game Development : Build 7 2D & 3D Games: https://www.udemy.com/unityandroidgamecourse/?couponCode=YOUTUBE9 | -------------------------------------------------------------------- | Download My Games: 1. Line Runner: https://play.google.com/store/apps/details?id=com.chargergames.linerunner 2. Dodging Dog: https://play.google.com/store/apps/details?id=com.ChargerGames.DodgingDog 3. 3D ZigZag Racer: https://play.google.com/store/apps/details?id=com.ChargerGames.ZigZagRacer3D | -------------------------------------------------------------------- | Move To Any Specific Part Of The Video(Click Below): 1. 00:00 - Intro 2. 00:25 - Creating First Script 3. 00:55 - Destroying Objects 4. 02:07 - Detecting Mouse Clicks 5. 03:00 - Taking Keyboard Inputs 6. 03:45 - Adding Physics 7. 04:34 - Adding Force 8. 05:14 - Moving Object with Velocity 9. 06:14 - Detecting Collisions 10. 08:37 - Loading New Level 11. 10:38 - Displaying Text On Screen 12. 12:33 - Move Player with arrow keys | -------------------------------------------------------------------- | Join our Discord Server: https://discord.gg/53FcvdS Chat with fellow Game Devs & Stay Connected Other C# Videos: 1. Learn C# for Unity in 1 Hour : https://www.youtube.com/watch?v=MX_KK7u53As 2. C# in One Video (4 Hours) : https://www.youtube.com/watch?v=7gX-M26Oj1Y 3. Unity C# Essentials in 1 Hour : https://www.youtube.com/watch?v=VkT4gIsr4JY | -------------------------------------------------------------------- | List Of My Youtube Tutorials: 1. C# Scripting Tutorial: http://bit.ly/2wGacFB 2. Android Game Development Tutorial: http://bit.ly/2q8gAPv 3. 2D Racing Game Tutorial: http://bit.ly/2f6JwHo 4. 2D Brick Breaker Game Tutorial: http://bit.ly/1NCvcBy | -------------------------------------------------------------------- | Video Description: In this video we go over the essentials of C# scripting for Unity Game development in just 15 Minutes. Welcome to Unity3D C# scripting tutorial series. In this series you will learn how program in C# in unity to create awesome games. Writing scripts is one of the most important process of creating games in unity3d game engine. If you learn C# from ground up , you can create amazing games in unity. C# coding gives you a lot of control over unity, which you can use to do whatever you want in your game. If you are starting with unity game development scripting, you should start learning C# instead of javascript. C# allows you to write scripts for unity in a more organised way, which would help you to understand your game code later. If you want to be a game programmer then Unity and C# is a great way to start learning about various game development principles. Unity is a full fledged game engine, you can do anything you want, and when you have the power of C# in it, you can achieve anything easily. You can write more robust, readable, faster executable code for unity game development. You may have seen that Unity official tutorials also use C#, since it's better to write code in C# than javascript. javascript is a more free-form language, it gives you more freedom which is not that good in long run. If you want to write better code for unity game development, then you should write your code in unity with C#. C# is a great language developed by Microsoft for it's .net platform, and unity uses it as a scripting language. Thanks a lot for watching this video, if you have any questions, write those in comments. I hope you enjoyed. Please subscribe and like , comment , share. Thank you very much. :)