Posts Tagged ‘Class’

Using C# delegates in Unity3D scripts

Click here to read Using C# delegates in Unity3D scripts

When Unity3D 3.0 came out, it not only fixed a lot of bugs and added features, but it also upgraded the Mono version being used, including C# language features like namespace support, linq and delegates. This post is going to be about the latter, explaining what is a delegate and what benefits it could bring when using it to develop games on Unity3D.

Basically, in C#, a delegate is a reference to a method or to a group of methods that have the same signature (returns the same type and has the same parameters). A better explanation can be found at Microsoft’s MSDN C# documentation: (more…)

Unity3D: JavaScript vs. C# – Part 5

Click here to read Unity3D: JavaScript vs. C# – Part 5

As the end of the year draws close, so is this post series. Here, the differences between JavaScript and C# when casting a ray in Unity3D will be pointed out. Don’t forget to read the first, second, third and forth parts of the series for a better general understanding of what is being discussed here.

Let’s start from the basics: What is ray casting? As the name describes, it is basically a program that simulates a ray being cast, much like a laser pointer in real life. It is very useful for game programming, as Raycast classes are programmed to return the distance a ray collided with something (and sometimes, even the name of the object). Unity3D doesn’t have one single Raycast class, instead its functionality is scattered across the Physics, RaycastHit and Ray classes.

(more…)

Unity3D: JavaScript->C# or C#->JavaScript access

Click here to read Unity3D: JavaScript->C# or C#->JavaScript access

When programming for Unity3D, there are some cases where we need to access a script written in another programming language that isn’t the one we are currently using. Although it is highly recommended to convert all scripts to the same one, it is useful to know how to access a C# script from a JavaScript class and the other way around.

The first thing to do is place the scripts in the correct folders in your Project tab. The script you want to have access must be inside the Standard Assets or the Plugins folder. The other script has to be placed outside these folders. After this step, just call the GetComponent() method as any other Component. Here’s a JavaScript example: (more…)

Unity3D: JavaScript vs. C# – Part 4

Click here to read Unity3D: JavaScript vs. C# – Part 4

This is the 4th post of a series that tries to explain the main differences between JavaScript and C# when programming for Unity3D. In this post, some differences between yielding (pausing) code execution in these two programming languages will be pointed out. Before continuing, it is highly recommended that you read the first, second and third other posts of the series.

As explained before, yielding pauses the code execution, being very useful to game programming, as you have a better control when things will happen in your game. Whether using C# or JavaScript in Unity3D, one can’t simply yield the Update() method. There is a workaround for that, but as you might have guessed, they are different for the two programming languages we are discussing. Since these workarounds are often used, they will be the examples presented on this post. Let’s start by seeing how to yield a JavaScript code: (more…)

Unity3D: JavaScript vs. C# – Part 3

Click here to read Unity3D: JavaScript vs. C# – Part 3

This is the third part of a series that show some of the differences between JavaScript and C# when writing scripts for Unity3D game engine. I suggest that you read the first and second post of the series to better understand what is going on here.

In this third part, I will point out some differences between JavaScript and C# by writing a script that makes a GameObject move forward. So, let’s start with the programming language that will take the smallest number of lines to make a GameObject move, JavaScript: (more…)