EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 280+ Courses All in One Bundle
  • Login
Home Design Design Tutorials Design Basic Tutorial Unity Scripting
Secondary Sidebar
Design Basic Tutorial
  • Basics and Advanced
    • Pro Tools Version
    • What is Autodesk BIM?
    • Animation Character
    • Autodesk Pixlr
    • Sketch plugins
    • Graphic designer requirements
    • Online Gaming Platforms
    • InDesign eyedropper tool
    • InDesign rounded corners
    • InDesign align
    • InDesign resize image
    • InDesign scripts
    • InDesign export to word
    • InDesign newsletter templates
    • InDesign Magazine
    • InDesign Missing Fonts
    • InDesign line spacing
    • InDesign Fonts
    • InDesign Preview Shortcut
    • InDesign Extension
    • InDesign columns
    • InDesign overset text
    • InDesign data merge
    • InDesign grid template
    • InDesign layers
    • InDesign reset preferences
    • InDesign master pages
    • InDesign layout
    • InDesign portfolio template
    • InDesign invoice template
    • InDesign word count
    • InDesign underline
    • InDesign superscript
    • InDesign presentation templates
    • InDesign drop cap
    • InDesign how to add page numbers
    • InDesign resume template
    • InDesign import pdf
    • InDesign table of contents
    • InDesign change page size
    • InDesign book templates
    • InDesign link text boxes
    • InDesign vertical text
    • InDesign Bullet Points
    • InDesign center text vertically
    • InDesign newspaper template
    • InDesign shortcuts
    • InDesign gap tool
    • InDesign paragraph styles
    • InDesign Grid
    • InDesign margins
    • InDesign highlight text
    • InDesign Wrap Text
    • ZBrush ZRemesher
    • Animation Software
    • Principles of Animation
    • 3d Animation Software
    • 3D Modeling Software
    • 3D Software Design
    • 2D Animation Software
    • 10 Quick Tips For Adobe InDesign Users
    • ADOBE FLASH CS6
    • Affinity Photo Brushes
    • Adobe Lightroom For Free
    • Lightroom Background Color
    • Lightroom?Shortcuts
    • Lightroom XMP
    • Lightroom editing
    • Masking Layers in Adobe After Effects
    • Adobe Premiere Pro Plugins
    • Adobe Speedgrade
    • Install Adobe Creative Cloud
    • Install Adobe Premiere Pro
    • What is Adobe Creative Cloud
    • What is Adobe Illustrator
    • Adobe InDesign Templates
    • What is Adobe InDesign
    • Adobe Indesign Uses
    • InDesign Background Color
    • What is Adobe Lightroom
    • What is Adobe Muse
    • What is Adobe Prelude
    • What is Adobe Premiere Pro
    • Careers in 3D
    • Careers in 3D Modeling
    • Careers in 3D Printing
    • Best Editing Software
    • Video Editing Application
    • Video Editing Tools
    • Career In Designing
    • Careers in Game Development
    • 3D Printing Models
    • Adobe Editing Softwares
    • Types of Adobe
    • Types of Illustration
    • What is Graphic Design
    • Best Graphic Design Software
    • Best Photo Editors
    • Game Testing Interview Questions
    • VFX Softwares
    • Unity Scripting
    • Unity Principle of Design
    • Unity JSON
    • Unity webGL
    • Unity 2D
    • Unity 3D Games
    • VFX Companies
    • How To Use Indesign
    • New Features InDesign CS6
    • What is Houdini?
    • Advantages of 3D Printing
    • What is Autodesk
    • Autodesk Character Generator
    • Autodesk Vault
    • Autodesk Viewer
    • Autodesk Navisworks
    • Autodesk Desktop Connector
    • Designers and Architects
    • 2D and 3D Shapes
    • Keyboard Shortcuts for Premiere Pro
    • Unreal Engine Tutorial
    • Best Audio Editors
    • Digital Image Processing
    • What is Motion Graphics
    • What is Digital Design
    • Design Pattern Interview Questions
    • Graphic Design Interview Questions
    • UX designer Interview Questions
    • Is Unreal Engine Free
    • What Is Design Pattern
    • What is VFX
    • What is Audacity
    • What is Unreal Engine
    • What is CATIA
    • What is Blueprint
    • Sony Vegas Pro for Free
    • 3D Graphics Software
    • Best Online Photo Editor
    • Photo Editing App
    • Vector Graphics Editor
    • ArchiCAD
    • ArchiCAD Shortcuts
    • Audacity Keyboard Shortcut
    • Davinci Resolve Shortcuts
    • What is pixel art?
    • Picmonkey Alternatives
    • Adobe Flash Player Alternative
    • Pro tools shortcuts
    • Pro Tools Plugins
    • Architectural Design Software
    • Architectural Design Principles
    • Architecture Design Types
    • Architectural design tools
    • Application of Digital Image Processing
    • Digital Image Processing Fundamentals
    • OpenGL Versions
    • Revit Keyboard Shortcuts
    • Revit plugins
    • Indesign version
    • What is DirectX?
    • DirectX Versions
    • Cinema 4D Plugins
    • Cinema 4d shortcuts
    • Godot Engine
    • Godot tilemap
    • Krita Animation
    • Krita Software
    • Krita Shortcuts
    • Krita eraser
    • Krita brushes
    • Krita Pixel Art
    • Krita Plugins
    • Skencil
    • Fusion 360 Keyboard Shortcuts
    • Anim8or
    • Affinity photo plugins
    • What is Affinity Photo?
    • FL Studio Plugins
    • Vectorworks
    • Architectural Design Elements
  • Inkscape
    • What is Inkscape?
    • Inkscape layers
    • Inkscape Crop
    • Inkscape text
    • Inkscape G-code
    • Inkscape mirror
    • Inkscape rounded rectangle
    • Inkscape EPS
    • Inkscape image to vector
    • Inkscape unlock
    • Inkscape Curved Text
    • Inkscape laser cutting
    • Inkscape background color
    • Inkscape arrow
    • Inkscape vector
    • Inkscape animation
    • Inkscape 3D
    • Inkscape SVG
    • Inkscape Dark Theme
    • Inkscape trace bitmap
    • Inkscape gradient
    • Inkscape Greek Letters
    • Inkscape shadow
    • Inkscape snap to grid
    • Inkscape Fonts
    • Inkscape extensions
    • Inkscape PNG to SVG
    • Inkscape Art
    • Inkscape transparent background
    • Inkscape Alternatives
    • Inkscape Remove Background
    • Inkscape text to path
    • Inkscape logo
  • Figma
    • Figma Libraries
    • Figma fonts
    • Figma Plugins
    • Figma grid
    • Figma Alternatives
  • SketchUp
    • SketchUp Keyboard Shortcuts
    • SketchUp geolocation
    • SketchUp animation
    • SketchUp Change Dimensions
    • SketchUp Import
    • SketchUp 2D
    • SketchUp rendering
    • Sketch software
    • Figma Shortcuts
    • SketchUp Layers
    • SketchUp landscape design
    • SketchUp group
    • SketchUp intersect faces
    • SketchUp Rotate
    • SketchUp Architecture
    • SketchUp 3D Models
    • SketchUp Vray
    • SketchUp Units
    • SketchUp garden design
    • SketchUp Layout
    • SketchUp Mirror
    • SketchUp Extensions
    • SketchUp Unhide
    • SketchUp kitchen
    • SketchUp background
    • SketchUp library
    • SketchUp dynamic components
    • SketchUp CutList
    • SketchUp Array
    • SketchUp Woodworking
    • SketchUp resize component
    • SketchUp Ungroup
    • SketchUp Deck Design
  • GIMP
    • What is GIMP
    • GIMP Versions
    • GIMP brushes
    • GIMP line tool
    • GIMP blend tool
    • GIMP clone tool
    • GIMP magic wand tool
    • GIMP 3D Text
    • GIMP healing tool
    • GIMP Layers
    • GIMP Fonts
    • GIMP Toolbox
    • Blur Effect in GIMP
    • GIMP Change Background Color
    • GIMP Floating Selection
    • GIMP?batch processing
    • GIMP Crop
    • GIMP Filters
    • GIMP GMIC
    • GIMP?opacity
    • Animation in GIMP
    • GIMP import image
    • GIMP replace color
    • GIMP text outline
    • GIMP video editing
    • GIMP Alternatives
    • GIMP vector graphics
    • GIMP text shadow
    • GIMP resize image
    • GIMP pixel art
    • GIMP invert colors
    • GIMP GIF
    • GIMP move selection
    • GIMP normal map
    • GIMP remove background
    • GIMP rotate
    • GIMP grid
    • GIMP text effects
    • GIMP erase to transparent
    • GIMP resize layer
    • GIMP Patterns
    • GIMP extensions
    • GIMP blur background

Related Courses

Maya Course

Photoshop course

After Effects Course

Unity Scripting

Unity Scripting

Introduction to Unity Scripting

In order to give life to the assets in the project, we need scripting. It’s the most fundamental part of the application you want to build using Unity Scripting. Scripts are used to write the logic of how the Game Objects should behave in the application. It can be used to create different AR, VR systems, Graphics effect, animation control, physics control, custom AI system, etc. So overall speaking, scripting is the heart and soul of the application.

How to Use Unity Scripting?

Unity 3D uses Mono behaviour to inherit from in its scripts. It supports C# natively. It is the most widely used scripting language in unity. However, we can write scripts in many other .NET languages if they can compile compatible DLL. So, for now, we will stick to the C# way of scripting.

Start Your Free Design Course

3D animation, modelling, simulation, game development & others

All in One Design Bundle(280+ Courses, 100+ projects)
AutoCADHoudiniAdobe Premiere ProPhotoshop
SolidworksVFX Visual EffectsCorelDrawUnreal Engine
Price
View Courses
280+ Online Courses | 100+ projects | 2000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (64,570 ratings)

Steps of Creating Unity Scripting

Let’s learn the steps of creating and using the scripts.

1. Creating the Scripts

  • Navigate to the Project Right-click Create > C# Script.
  • Another way is by selecting Assets > Create > C# Scriptfrom the main menu.

Unity Scripting

  • A new script will be created, and it will prompt us to enter the new name.
  • Give a proper name to the script and hit enter. Our first script is created successfully.

Unity Scripting

2. Choosing the Script Editor

  • Unity uses Visual Studio as the default script editor.
  • To change the script editor, go to Edit> Preferences > External Tools.
  • Browse for the file location of the Script Editor.

Unity Scripting

3. Script Anatomy

  • Different features in Unity can control the GameObject behaviors called components.
  • Other than these components, we can implement this by using scripts if we need any specific feature.
  • When we double click on the script, the script editor will open.
  • The structure of the script will look like below.

Code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MyFirstScript: MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}

  • All the scripts we write inside unity will be derived from the built-in class called Mono behaviour.
  • It’s like a blueprint of the new custom component that can change the Game Object behavior.
  • Each time it creates a new instance of the script object, you attach it as a component to the Game Object.
  • The class name of the script will be picked from the file name we gave to create the script.
  • To attach the script to the Game Object, make sure that the name of the class and the name of the file are the same. Or else it will give us a compilation error, and we won’t be able to attach this script to Game Object.
  • Here we can see that there are two functions that are created by default. They are Start and Update.
  • The start function is used to initialize the things inside the script before we use them.
  • The start function is called at the beginning of the Gameplay if the script is enabled in the inspector window of the Game Object. It is called only once per execution of the script.
  • The update function will be called every frame. Basically, this is needed if something to be handled in each and every frame. It can be movement, taking user input, triggering actions, etc.
  • We can write our custom functions, similar to the way Start and Update functions are written.

4. Attaching the Script to the Game Object

  • Select the Game Object to which the script needs to be attached.
  • The first way is to directly drag and drop the script to the Inspector window of the Game Object. Here we will attach it to the Main Camera.
  • The second way is to click on the Add Component, start typing the script name, and select the script.

 Game Object    Game Object

  • It will be attached to the Game Object, as we can see it in the inspector window.

 Game Object

5. What are these Variables, Functions, and Classes?

  • Variables: Variables are like containers to hold value or references of the objects. According to the convention, the variable name will start with the lower case letter.
  • Functions: Functions are the snippet of code that uses the variables and the additional logic to change the behavior of the objects. They can modify these variables as well. Functions start with the Upper case letter. It’s a good practice to organize our code inside a function to increase readability.
  • Classes: Classes are the collection of variables and functions to create a template that defines the object property. The class name is taken from the file name we give at the time of creating the script. Usually, it starts from the Upper case letter.

6. Giving References to an Inspector

  • The variables which are declared as public in the class will be exposed to the inspector, and we can assign any references or value to them.
  • Consider the below code:

Code:

public class MyFirstScript : MonoBehaviour
{
public GameObject cameraObject;
// Start is called before the first frame update
void Start()
{

}
}

  • Here camera object is the public variable that needs a reference. Go to the unity editor. Select the object, and if we look at the script component, we can see an empty space (None (Game Object)) like below for assigning select the Game Object from the hierarchy and drag and drop in the Camera Object box.

7. Access the Components

  • There will be scenarios wherein we need to access different components attached to the Game Object.
  • Let’s take an example of accessing the Camera component of the Game Object.
  • Get Component; we will get the type Camera.

Code:

void Start ()
{
Camera cameraObject = GetComponent<Camera>();
Debug.Log("Camera Object is: " + cameraObj);
}

8. Modify the Component Values

  • Modifying the component values is the next step to the above example.
  • The below code explains how we can modify the camera Field of View.
  • After we hit the play button in Editor, we can see that the camera Field of View will change from default value 60 to 90 in the inspector menu of the camera.

Code:

public GameObject cameraObject;
private int cameraFOV = 90;
void Start ()
{
Camera cameraObj = GetComponent<Camera>();
cameraObj.fieldOfView = cameraFOV;
}

Conclusion

In this article, we have seen about Unity Scripting, the need for the scripting, step by step to create the script and different script components, access the components through the script and modify them.

Recommended Articles

This has been a guide to Unity Scripting. Here we discuss the introduction and steps to create a Script with examples for better understanding. You can also go through our other suggested articles to learn more –
  1. Shell Scripting Commands
  2. What is Shell Scripting?
  3. What is Cross-Site Scripting?
  4. How to Install Unity?
Popular Course in this category
Unity Scripting Tutorial (3 Courses)
  3 Online Courses |  11+ Hours |  Verifiable Certificate of Completion |  Lifetime Access
4.5
Price

View Course

Related Courses

Maya Training (10 Courses, 7+ Projects)4.9
Photoshop Training (10 Courses, 12+ Projects)4.8
After Effects Training (3 Courses, 3+ Projects)4.7
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Photoshop Tutorial
  • 3ds Max Tutorial
  • All Tutorials
Certification Courses
  • All Courses
  • Design Course - All in One Bundle
  • Photoshop Course
  • Maya Course
  • Unreal Engine Course
  • Unity Course
  • DaVinci Resolve Course
  • Pro Tools Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Design Course

Banner Creation, Object Selection, Sky Replacement, Advance Filters & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Design Course

3D animation, modelling, simulation, game development & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more