1. Updated Resource Submission Rules: All model & skin resource submissions must now include an in-game screenshot. This is to help speed up the moderation process and to show how the model and/or texture looks like from the in-game camera.
    Dismiss Notice
  2. DID YOU KNOW - That you can unlock new rank icons by posting on the forums or winning contests? Click here to customize your rank or read our User Rank Policy to see a list of ranks that you can unlock. Have you won a contest and still havn't received your rank award? Then please contact the administration.
    Dismiss Notice
  3. Travel to distant realms and encounter scenes unknown to the common folk. The Greatest of Adventures is upon us with the 8th Cinematic Contest. Join in on a fun ride.
    Dismiss Notice
  4. The 18th Icon Contest is ON! Choose any ingame unit and give him/her Hero abilities. Good luck to all.
    Dismiss Notice
  5. The Secrets of Warcraft 3 have revealed interesting works. The RESULTS for Abelhawk's Mini-Mapping Contest #15 have come out!
    Dismiss Notice
  6. Contestants are to create a scene set in the Stone Age. Come and see what you can come up with. We wish you the best of luck!
    Dismiss Notice
  7. Colour outside the lines! Techtree Contest #13 is a go. The contest is optionally paired.
    Dismiss Notice
  8. Night Rider gained several songs for his journey. The poll for the 12th Music Contest has started. Check it out!
    Dismiss Notice
  9. Greetings cerebrates, our Swarm needs new spawners that will have numerous children. Join the HIVE's 31st Modeling Contest - Spawners and Spawned! The contest is optionally paired.
    Dismiss Notice
  10. Check out the Staff job openings thread.
    Dismiss Notice
Dismiss Notice
60,000 passwords have been reset on July 8, 2019. If you cannot login, read this.

[Tutorial #2] Scripting

Discussion in 'Unity 3D' started by Statharas, Oct 16, 2012.

  1. Statharas

    Statharas

    Joined:
    Jul 9, 2008
    Messages:
    2,337
    Resources:
    14
    Tools:
    1
    StarCraft II Resources:
    5
    Tutorials:
    8
    Resources:
    14
    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 (Text):

    [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 (Text):

    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 (Text):

    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 (Text):

    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!
     
  2. Zelda.Alex

    Zelda.Alex

    Joined:
    Dec 7, 2008
    Messages:
    778
    Resources:
    0
    Resources:
    0
    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.
     
  3. Statharas

    Statharas

    Joined:
    Jul 9, 2008
    Messages:
    2,337
    Resources:
    14
    Tools:
    1
    StarCraft II Resources:
    5
    Tutorials:
    8
    Resources:
    14
    That is also true. You can open it via the Editor as well.
     
  4. Zelda.Alex

    Zelda.Alex

    Joined:
    Dec 7, 2008
    Messages:
    778
    Resources:
    0
    Resources:
    0
    @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?
     
  5. Statharas

    Statharas

    Joined:
    Jul 9, 2008
    Messages:
    2,337
    Resources:
    14
    Tools:
    1
    StarCraft II Resources:
    5
    Tutorials:
    8
    Resources:
    14
  6. Cihparg

    Cihparg

    Joined:
    Jul 4, 2008
    Messages:
    958
    Resources:
    1
    Models:
    1
    Resources:
    1
  7. Statharas

    Statharas

    Joined:
    Jul 9, 2008
    Messages:
    2,337
    Resources:
    14
    Tools:
    1
    StarCraft II Resources:
    5
    Tutorials:
    8
    Resources:
    14
    We get it. It's called JavaScript by most, but it's normally UnityScript.
     
  8. Zelda.Alex

    Zelda.Alex

    Joined:
    Dec 7, 2008
    Messages:
    778
    Resources:
    0
    Resources:
    0
    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?
     
  9. Statharas

    Statharas

    Joined:
    Jul 9, 2008
    Messages:
    2,337
    Resources:
    14
    Tools:
    1
    StarCraft II Resources:
    5
    Tutorials:
    8
    Resources:
    14
    Nah. You think of stuff you want to build, think of how they should work, and search for functions.

    For example, you want to make an object move. The object's transform is what manages it. So you'd search for Transform. You look there, and find that it has the function Translate. So basically, you use that.
     
  10. Zelda.Alex

    Zelda.Alex

    Joined:
    Dec 7, 2008
    Messages:
    778
    Resources:
    0
    Resources:
    0
    Ok. Thanks.
     
  11. Tr!KzZ

    Tr!KzZ

    Joined:
    Mar 7, 2005
    Messages:
    800
    Resources:
    29
    Models:
    18
    Icons:
    2
    Maps:
    9
    Resources:
    29
    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.
     
  12. Zelda.Alex

    Zelda.Alex

    Joined:
    Dec 7, 2008
    Messages:
    778
    Resources:
    0
    Resources:
    0
    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.
     
  13. Statharas

    Statharas

    Joined:
    Jul 9, 2008
    Messages:
    2,337
    Resources:
    14
    Tools:
    1
    StarCraft II Resources:
    5
    Tutorials:
    8
    Resources:
    14
    Actually, you CAN make your code in JavaScript and THEN turn it into C# for performance.
     
  14. Zelda.Alex

    Zelda.Alex

    Joined:
    Dec 7, 2008
    Messages:
    778
    Resources:
    0
    Resources:
    0
    Using some software to convert them or manually?
     
  15. Statharas

    Statharas

    Joined:
    Jul 9, 2008
    Messages:
    2,337
    Resources:
    14
    Tools:
    1
    StarCraft II Resources:
    5
    Tutorials:
    8
    Resources:
    14
    Manually. Never trust a machine to convert code or words.
     
  16. Zelda.Alex

    Zelda.Alex

    Joined:
    Dec 7, 2008
    Messages:
    778
    Resources:
    0
    Resources:
    0
    So the syntax in Unity is similar for the two?
     
  17. Statharas

    Statharas

    Joined:
    Jul 9, 2008
    Messages:
    2,337
    Resources:
    14
    Tools:
    1
    StarCraft II Resources:
    5
    Tutorials:
    8
    Resources:
    14
    They share some aspects and, of course, monobehaviour, but they are fairly different.
     
  18. Tr!KzZ

    Tr!KzZ

    Joined:
    Mar 7, 2005
    Messages:
    800
    Resources:
    29
    Models:
    18
    Icons:
    2
    Maps:
    9
    Resources:
    29
    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: Oct 26, 2012
  19. Statharas

    Statharas

    Joined:
    Jul 9, 2008
    Messages:
    2,337
    Resources:
    14
    Tools:
    1
    StarCraft II Resources:
    5
    Tutorials:
    8
    Resources:
    14
    Actually, you do it sorta after you complete a system.
     
  20. Zelda.Alex

    Zelda.Alex

    Joined:
    Dec 7, 2008
    Messages:
    778
    Resources:
    0
    Resources:
    0
    @Tr!kzZ
    I have programmed in C so using C# will not be difficult if the syntax is so similar.

    What are you talking about?