Creating a C# Script | Unity Tutorial

Do you want to customize the behavior and appearance of your game objects? You can use script to do just that. This tutorial will be the first of a series in which you will learn how to code a C# script for Unity. If you are a beginner and want to learn how to build virtual reality games, check out our Unity3D course

Setting up the Scene

Create a new project in Unity named “CodeIntroduction”. Create a cube in the Hierarchy. Give Cube the position 0 0 0 so that it is centered in the Scene.

Let’s change Cube’s color. Create a material named “ColorMaterial” in the Assets folder. In the Inspector, change the material’s color to an orange with RGB values 155 153 0.

Drag and drop ColorMaterial from Assets to Cube in the Scene to apply the color to the cube. Set Main Camera’s Z position to -4 so that it is closer to Cube.

Save the Scene as “Game.unity”. A file named “Game” will appear in the Assets folder.

Creating the Script

Every game object in the Hierarchy has components in the Inspector. Unity provides components such as materials, collisions, and 3D primitives. To create a more complex game, you can write custom behavior for an object. To this, you create a script and attach the script as a component of the game object.

In the Project window, let’s make a script for Cube. Right-click in Assets. Select Create > C# Script.

A new file will appear in Assets. Name the file “Cube”.

When you create or update a script, a wheel will spin in the bottom right corner of the Unity window. This wheel spins when a compilation occurs. A compilation takes lines of code and converts it to a format that is easier for your game to process.

When the compilation is complete, the script has been added or updated. Then you can run the game to see the changes the script applies.

Every time you change and save a script, Unity will re-compile the project to ensure that everything is correct. If there is a mistake in your code, such as a missing character or invalid variable name, a message will appear in the Console.

Currently, the Cube script will not execute any code because it is not attached to any object. Let’s attach the script to the Cube game object. Drag and drop the Cube script to the Cube game object in the Hierarchy or Scene. Alternatively, drag and drop the script to a blank space in the Cube object’s Inspector.

A Cube (Script) component will appear in Cube’s Inspector.

Double-click on the Cube script in Assets to open it. The file “Cube.cs” will open in MonoDevelop-Unity, as evident in the next image. MonoDevelop is a free, open-source coding interface that downloads when you download Unity if you do not unselect Unity’s default downloads.

Note that if you prefer to use other tools, such as Visual Studio, Sublime Text, or Notepad++, you can configure Unity to use that text editor by default.

The first line in the script is using UnityEngine;. This line means that the code in the script can use items already implemented in the Unity engine.

using System.Collections; means that the script can use certain parts of the code that are already written in the C# environment for collecting objects.

The other code in the file defines the Cube class. public class Cube defines a public class called Cube. : MonoBehaviour means the class extends MonoBehaviour, which is the base class for a game object. The code we write between the curly braces will change how Cube behaves.

The default methods in the Cube class are Start and Update. The Start method executes code when the game begins. The Update method executes code constantly. Want to learn more about methods? Check out our Unity3D course, where you build 30 virtual reality games! Don’t forget to save your project.

Build watchOS Apps for the Apple Watch | Course Release

Start now.

Do you want to build your own apps for the Apple Watch? Even if you have never coded before, you can build a watchOS app with our brand-new Apple Watch course.

You do not need any prior knowledge to take this course – it is perfect for beginners. The Apple Watch course begins by covering the fundamentals of the Swift programming language. You will learn how to use Xcode to create the layout and functionality of an iOS app.

If you do have experience developing for iOS, the Apple Watch course is your head start in developing for watchOS. You will learn the differences between coding for iOS and watchOS.

To learn how to develop for watchOS, we will create an Apple Watch app of our own in Xcode – from scratch. You will learn how to:

  • Add a basic label to create the layout of your app
  • Add and implement dates and timers
  • Insert switches, sliders, and pickers
  • Create different tables and context menus
  • Insert images and movies into your Apple Watch apps
  • Enable notifications

By the end of the Apple Watch course, you will have a functioning app that you can expand on to add your own ideas. You will be able to test the app like a user would. Let’s go!

Learn JavaScript in 1 Hour! Book Release

All you need is 1 Hour.

Do you want to learn the foundations of computer programming? Are you tired of being bogged down by confusing jargon and complex descriptions? We have the book for you: Learn JavaScript in 1 Hour.

Learn JavaScript in 1 Hour is an easy-to-understand introduction to JavaScript. With this book, your website will be up-and-running in no time!

Important topics in web development are shown with examples that are easy to read. If you have a Mac computer, PC, or even a mobile device, you can follow along with the book’s examples on JSBin is an editor that allows you to see your creations in real time.

Learn JavaScript in 1 Hour covers topics including:

Functions – Finally your website can do things! It’s not just a pretty face. Learn how to enable your site to perform tasks.

Variables – Integers, doubles, floats, characters, strings: these are all different types of variables. Variables are crucial to programming because they contain data. Learn about the different types and when you should use each one.

If Statements – If you read Learn JavaScript in 1 Hour, you will reach the section on conditionals. If statements run code IF a specific condition is met. With an if statement, you can test a value, run certain code only when you want to, and prevent your site from crashing!

If you want to learn how to give a website functionality, Learn JavaScript in 1 Hour can teach you the foundations of using JavaScript. Even if you have never coded before, you can follow along. For more books on tech topics, visit our Lulu shop.

Making a Cube | Unity Tutorial

To prototype a game in Unity, you can design a scene using primitives. To learn how to build 30 virtual reality games in Unity, check out our Unity 3D course! There are a number of primitives, or elements, that you can create in Unity, such as a cube.

Later, you can create more complex 3D elements in modeling programs, such as Blender. Then you can import the art into Unity.

Creating a 3D Primitive

To make a 3D element, right-click in the Hierarchy. A menu will appear containing listing game objects you can create. Clicking “Create Empty” would create a net game object, which has only a Transform component.

In the “3D Object” tab, there are names of three-dimensional shapes. Click on “Cube”, as in the next image.

“Cube” will appear in the Hierarchy. A cube will appear in the Scene, as you can see in the following image.

Cube’s Components

In the Inspector, there are many components for Cube. To position the cube in the center of the scene, change the X, Y, and Z Position values to 0.

If you change Cube’s X Scale value, Cube’s size will change in the X axis. Changing the Scale’s Y value makes the cube taller or shorter. The Z Scale value corresponds to the cube’s size in the depth axis, as evident in the following screenshot.

To change Cube’s scale visually, click on the fourth icon in the top left of Unity. Colored cubes will extend from Cube’s center. You can drag the cubes to change Cube’s size. If you click and drag the gray cube in the center of the cube object, Cube’s size will change in all three axes at once.

The Cube (Mesh Filter) component is the data for the 3D object that is rendered on the Scene. The Box Collider is used to process collisions.

The Mesh Renderer draws the cube. If you disable the Mesh Renderer by un-checking it, the cube will become invisible.

To learn more about the Mesh Renderer and Unity, check out our Unity 3D course, where you build 30 virtual reality games!

Learn to Code in Python | New Course Release

Learn to code. Right here, right now.

If you’ve always wanted to learn to code, our new Python course is the place to start. In this course, you learn how to code in the Python 3.5 programming language. Even if you have never coded before, you can learn how to use Python.

Python is easy to understand and can be used for many different environments. Cross-platform apps and 3D environments are often made in Python.

Our Python course teaches you the foundations of coding in Python. This course does not assume any level of experience and is therefore perfect for beginners!

The course covers basic programming concepts for people who have never programmed before. You will learn key topics in Python and coding in general, including variables, loops, and classes. By the end of the course, you will know how to handle input, output, and errors.

To learn how to use Python, we will create our own functioning Blackjack game! In this game, a player will receive cards, submit bets. Python will keep track of the player’s score.

You will be able to use the practical examples in the Python course to make your own apps and environments. Python is a popular programming language that is useful to know because of its versatility. Let’s get started!