Skip to main content

A Few Pointers On Switching From JavaScript To C#

One does not simply add to an array.

In JavaScript you might have familiarity with using arrays.
You might also be familiar with "adding" to arrays.
Well Unity sugarcoated that for you.
Arrays are not really re-sizable.
When you declare an array in c#,  you have to define it right there.

If your looking for something similar to re-sizable arrays, use lists.
To use Lists in Unity c#, you must add the line
"using System.Collections.Generic;" At the top of your code.

Declaring a list of Game Objects would go something like this
List<GameObject> myGameObjectList = new List<GameObject>();

Then adding to the list would is as simple as
myGameObjectList.Add(gameObjectInstance);

Then if you really just want an array you would say something like
GameObject[] gameObjectArray = myGameObjectList.ToArray();

The API is not what it always seems.

Unity has also sugar coated some of its API, some parts to the point where its practically lying to you in the provided documentation. I learned this the hard way in a short game I did in JavaScript a while back, I had blood splats come up on the screen, and then fade away. To have a fade property, I had the blood splats change their GUI alpha value gradually to 0.

Sometimes when you think your going crazy, simply go to Google.

Everything is a class

In the begging of your coding experience, you were probably not familiar with classes. In fact in "real" JavaScript you can not simply declare a class. Unity had made it though, so that you can declare a class in a similar way compared to the rest of it's syntax.

So now in c#, when you create a c# script in Unity and open it in MonoDevelop(or what ever editor your using), you will see now at the top you have something like:
public class ScriptName : MonoBehaviour {}

***Make sure you script name matches your class name!!!!***

Example, if you have created a file in Unity named Example.cs, in your code  you must have
public class Example: MonoBehaviour {}
If you do not do this your compiler will throw you back an error.

Declaring Methods and Returning Variables

Like in all Object oriented languages declaring methods follow the same flow in syntax. In JavaScript, declaring methods is simply typing function(){}. C# follows the same flow except you use VOID instead of function.

But this is not always the case in C#! In Javascript, methods labeled function could return variables. In order for a method in C# to return variables, two things must occur

1) Void must be replaced with the type of variable you want to return.
2) The method must be declared public for other classes to call it!

You can not anymore say:

void GetPlayersHealth(){
    return Health;
}

You need to now say:

public int GetPlayersHealth(){
    return Health;
}

Declaring variables for Inspector View

With JavaScript all variables declared at the top of the script, unless declared as private, where shown in the inspector view in Unity as variables to manipulate. For C# you need to declare the variable as public or it will not show up in the Inspector pane in Unity's Editor.

Getting Instances of Other Scripts and Components

Let's say you want to get an instance of the players behavior in order to add damage to it. To get it as a variable in the code your currently working on, you now must use the needed class name as the variable type. Getting components is also different, as in the way you call it. In JavaScript it might look like this

var playerBehavoirInst = GameObject.Find("Player").GetComponent(PlayerBehavior);

Now in C#, its change to 

PlayerBehavior playerBehaviorInst = GameObject.Find("Player").GetComponent<PlayerBehavior>();

Notice the get Component and how it's called.


Comments

Popular posts from this blog

How To Make a Hellish Looking Sky Box

I came across this problem while constructing my scene of Hell in a little project I've been working on, and could not find a reasonable sky box on the web for what I want. Maybe I was not looking hard enough, but I ended up making nice substitute. If you think the sky box looks familiar, then your right. The Sky box I'm using is already packaged with Unity3D! To import the sky boxes Unity has made for you,  simply go to Assets>Import Package>Skyboxes.  The sky boxes will appear in your projects tab under a folder named "Standard Assets". To make this sky box, first you must find the folder containing all the sky box materials and open it up. In it will be a list of sky boxes for your disposal. To get this skybox, I decided to tweak the "StarryNight Skybox" (But the "MoonShine Skybox" looks pretty cool also!).  Select the sky box and view it under the inspector tab. Underneath the properties there will be a tint color variable allowin...

How To Make A Gun Shot Sound (SFX On Unity 3D)

When it comes to audio in Unity, there are four components: Audio Clip , Audio Source , Audio Listener , and Audio Re-verb Zone . Audio Clips are the actual audio file imported into your game. Unity supports file formats: .aif, .wav, .mp3, and .ogg. When imported, you can compress them greatly, with the price of loosing some quality. You can do this by first selecting the audio clip, view it in the inspector. Under the Audio Importer component, you can switch the audio format from Native to the audio clip, to a compressed format applied by Unity. You can change how compressed the file is by dragging the bar at the bottom, then hitting apply. You can get plenty of free good SFX from a site called  freesound.org . All you have to do is create an account for free , and download all the sounds you want. I found a nice gun shot sound here . Simply download and load into your Project. Audio Source actually plays the audio clip in your scen...

Handling Music and Sound Effects In Your Games

Initiative  While developing Treva's Adventure I had to figure out a way to handle multiple music tracks and sound effects in a clean manner or suffer horribly.  What was going to help me achieve a simple solution was taking all the different sounds and centralizing them in a single class in order to black box them.   Any other code trying to play a sound wouldn't even know the sound file's name.   All code trying to play a music track would reference a enum that defines all the track names. Defining The Class Creating The Enum When I first started defining types in my enumeration,  I was naming the types to be exactly like the file name.  For a scary sound effect I had found a file named "ghost breath".  So around my code would be scattered lines like SoundManager.Play(SoundEffectType.GhostBreath);  This was fine until I found a sound that better fit the situation it was being used in,  and decided to use "ghost breath" for a...