• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[Tutorial #2] Scripting

Alright, by now you should have read the first tutorial, located here.

I am considering you have a bit of experience with any programing language, thus this is a bit "advanced".

We'll be doing this in JavaScript, so, go to your project view and create a JavaScript file.

It really is not required to have ACTUAL JavaScript knowledge. I've learned it while working in Unity.

First of all, keep the Script Reference in mind. It's the best tool you can have in Unity.

Write the following on your script, save it and drop it on an object.
A script needs to be attached to an object to be run.

Code:
[URL="http://docs.unity3d.com/Documentation/ScriptReference/Debug.Log.html"]Debug.Log[/URL] ("Hello");
The script above should have created a notification at the bottom left of your UI, saying "Hello".

That's good, for starters.

Everything you write outside a function is run once, thus your line was run only once.

Debug is a class. Classes contain different functions, such as Log, LogError, and more. You can find them all in the Reference.

Let's take this up a notch.

Code:
function Update(){
[URL="http://docs.unity3d.com/Documentation/ScriptReference/Debug.Log.html"]Debug.Log[/URL] ("Hello");
}
The Update function is run on every frame.
Certain predefined functions are run on certain times, such as OnGUI() being run twice, in the first call, Unity builds the GUI and draws it.

Let's add some variables.
Code:
var derp:int=0;
function Update(){
Debug.Log(derp);
}
When you save this script, Unity will have a new variable for you to edit in the Inspector. Toy around with it, it will spam you with the value you put in.

var always has to be there when declaring a variable.
derp is the name of your variable.
:identifier/component is used to give the engine the type of your variable. Normally, it is not needed, but it saves the engine a lot of time from trying to figure out what it is.

=0 is not needed either, but by adding this, you can have a predefined value for your variable.


Alright, let's do real stuff now.

Code:
var loc:[URL="http://docs.unity3d.com/Documentation/ScriptReference/Vector3.html"]Vector3[/URL]=Vector3(0,0,0);
var a:Transform;

function Update(){
loc=Vector3(loc.x,loc.y, loc.z+1);
//The variable above is a location on the axes. Using .x/.y/.z gives you the values inside the vector.
 Instantiate (a,loc, [URL="http://docs.unity3d.com/Documentation/ScriptReference/Quaternion-identity.html?from=Object"]Quaternion.identity[/URL]);
}
Instantiate clones the original object, places it at position and sets the rotation to rotation, then returns the cloned object. If a game object, component or script instance is passed, Instantiate will clone the entire game object hierarchy, with all children cloned as well.
The arguments it takes are a Transform, a Vector3(space in the axes), and a Quaternion. You should ignore the Quaternion for now.

A Transform variable is basically an object's transform. You need to assign an object to it, and the script will spam the same object over the Z axis.


That's it for now, I am willing to help people out, so drop your question via a reply!
 
Level 1
Joined
Dec 7, 2008
Messages
823
Wanted to add one thing. The complete Script reference is present with the unity installation. So, no need to check it always on internet.

My reference was present in this location on my computer:-
C:\Program Files (x86)\Unity\Editor\Data\Documentation\Documentation

Inside it there are many support resources. Inside the folder ScriptReferenceyou will find the script. Check other folders too.
 
Level 1
Joined
Dec 7, 2008
Messages
823
@Statharas
How do you find out the function that you are looking for in the reference? Suppose I want to make a script for making an object behave like a dice throw how do I go about finding the functions for that?
 
Level 1
Joined
Dec 7, 2008
Messages
823
That was helpful. But I wasn't trying to get you to give me the particular function. The dice throw was just an example. I wanted to know that how do you go about to find out the functions necessary for implementing a particular thing in the script reference? Is there some method or just google that?
 
Level 18
Joined
Mar 7, 2005
Messages
824
I would suggest to use c# instead. But this depends on the later use, if you're going to use it only as webplayer and interact with databases, etc. javascript would be the easier choice. As it doesn't complain about many errors, or generic codes.. while c# requires certain definitions to work properly.
 
Level 1
Joined
Dec 7, 2008
Messages
823
I would suggest to use c# instead. But this depends on the later use, if you're going to use it only as webplayer and interact with databases, etc. javascript would be the easier choice. As it doesn't complain about many errors, or generic codes.. while c# requires certain definitions to work properly.
If I want to make a proper game then I should use C# instead of Javascript? Ok. Is the C# inside Unity3D is the normal C# or some Unity C#?

Also thanks. I had started to think that it was just me talking with Statharas in this forum.
 
Level 18
Joined
Mar 7, 2005
Messages
824
I wouldn't suggest to convert stuff at the end, because you might end up with thousands of code-lines and trust me you don't want to convert all of them :p

There are automatic programs that might convert it for you, but if you get errors, because some specific lines are not converted correctly, have fun searching for them :p

---------------------------------
So in general I would suggest using C#, because it helps you learn faster. You need to write a bit more, but it works better and you have lot more opportunities that JavaScript doesn't support. So you will benefit from C#.
You can also create WebPlayer Games with it, as I already did. It could be, that the codes will be longer with some specific parts where you want to interact with some other sort of java or whatever, but I really don't know those facts as I never had problems with C#.

On the other hand, you have some super simple stuff with JavaScript on Unity as it is more integrated. You just need to type "var" to declare new variables, Unity doesn't care which one it is, it selects just the right ones for you. For C# you have to say what variable you want to use:

JavaScript: var : myCounter;
C#: int myCounter; (If you want to set a number: int myCounter = 0;)

So you need to say which type you want and then name it. while in JavaScript the myCounter could be used as anything.. Object, Integer, Bool, whatever.

I have to google some stuff to find out if there are some javascript only things, or any advantages to use it.. but as far as I noticed and worked with c# is just fine and also pretty easy.

(I don't want to talk bad about JavaScript, or your tuts Statharas, they're great! Just want to help a bit ;) )
 
Last edited:
Level 1
Joined
Dec 7, 2008
Messages
823
You complete a system for your game, and when you're satisfied with the way it works, you move it to C#.

You're satisfied with your AI Class? C#!
I think I will side with Tr!KzZ on this. If coding in C# is preferable for some reasons then it would be wiser to code in C#. Moving a code between two different languages is not something that I would like to do. That may cause big problems.

Does C# has some advantage over UnityScript in terms of performance?
 
Level 18
Joined
Mar 7, 2005
Messages
824
Sorry but I think it's just a waste of time to code with two languages.. just doing the first one and converting it to the other.. makes no sense for me, as you could do everything with both either.

C# is just less confusing when it comes to scripting, as you have to write additional stuff that makes it clear in use and the syntax isn't that hard (beside of that most of the people on the Unity3D Forums use it, so you'll find a lot of tuts about that)

And I guess you get some helping hands here and there if you want to create specific stuff you haven't learned, yet. or whatever ;)
 
Level 18
Joined
Mar 7, 2005
Messages
824
You can use other languages that may access the db instead (e.g. PERL), but php is preferred as it's easier and works well. I don't know about all the other possible languages you could use, I guess Java will work as well.

Here's a simpler version of it: http://wiki.unity3d.com/index.php/Easy_MySQL_Submission

(or you need to wait till i find some time to make the tutorial, it's very basic, easy and with lot less lines then those both tutorial links have ^^)
 
Level 18
Joined
Mar 7, 2005
Messages
824
As far as I can see JavaScript is more complicated.. needs more lines of code and stuff to be written to work. If you want to try web-based databases php is better.

I haven't used JavaScript at all, therefor I can't help you with tis, except using the better php method. Here's the Link I found: http://forum.unity3d.com/threads/28500-SQLite-Class-Easier-Database-Stuff maybe it can help u a bit understand the logic behind the JavaScript pat with the System.Data & mysql.data files

There was also a solution for JavaScript on the older links I gave you: http://wiki.unity3d.com/index.php?title=Server_Side_Highscores#JavaScript_-_HSController.js
 
Top