Search Unity

Newbie guide to Unity Javascript (long)

Discussion in 'Scripting' started by tonyd, Nov 9, 2009.

  1. groovfruit

    groovfruit

    Joined:
    Apr 26, 2010
    Posts:
    257
    Hello - and thanks for this guide, I'm often referring back to it :)

    I do have a question about your sample for Classes as follows:

    Code (csharp):
    1.  
    2. class Animal{
    3.     var name;
    4.     function Animal(n : String){ //this is the constructor
    5.         name = n;
    6.         Debug.Log(name + " was born!");
    7.     }
    8. }
    9.  
    10. cat = Animal("Whiskers");   //var keyword is optional when creating instances!
    11.  
    I've been trying to figure out why you have "name = n;", and why you couldn't have instead....
    Code (csharp):
    1. function Animal (name : String)
    Thanks for the advice!
     
  2. Chris-Sinclair

    Chris-Sinclair

    Joined:
    Jun 14, 2010
    Posts:
    1,326
    You could, but then the implementation of the constructor would have to look like:
    Code (csharp):
    1.  
    2. function Animal(name : String){ //this is the constructor
    3.     this.name = name;
    4.     Debug.Log(name + " was born!");
    5. }
    See the confusion?
     
  3. groovfruit

    groovfruit

    Joined:
    Apr 26, 2010
    Posts:
    257
    I can.... I need to let it absorb in the depths of my brain for a while though ;)
     
  4. poncho

    poncho

    Joined:
    Dec 10, 2010
    Posts:
    65
    lol, very short but useful guide to learn basics of programming
    nice work
     
  5. makan

    makan

    Joined:
    Jan 8, 2011
    Posts:
    342
    thank you so much for your awesome post :)
     
  6. FoxGirlTheGamer

    FoxGirlTheGamer

    Joined:
    Sep 29, 2010
    Posts:
    14
    Okay, so in the second scripting guide it says to 'Rename your script to Move1' But I can't rename it. Does anyone know what I'm doing wrong?
     
  7. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    What is happening when you try to rename the script? Is the new name not being accepted or does the text editor not appear for the name or... ?
     
  8. diegorborges

    diegorborges

    Joined:
    Feb 11, 2011
    Posts:
    23
    Thanks for the tutorial man! Really really useful.

    I'm happy that I started with Unity just last night and I already can at least do some very basic stuff... thanks to guys like you :)
     
  9. makan

    makan

    Joined:
    Jan 8, 2011
    Posts:
    342
    I thank you for your post, i read all of it but after the classes part i've found my self little lazy to understand them i'll try to improve my skills better by checking out your links :)
     
  10. Chub

    Chub

    Joined:
    Mar 14, 2010
    Posts:
    247
    Great guide that helped me get to brings with some Javascript, now finally I can make some games.
     
  11. producer2011

    producer2011

    Joined:
    Mar 8, 2011
    Posts:
    2
    This is a great lesson in Unity Java. I always found learning any programming language very confusing before I got in to 3D animation and Unity. Now I have to know this stuff !!!!!

    For me I'm not so good at dealing with abstract ideas, especially with math, so I need to first see a practical use for something before I can internalize it and then apply that knowledge on something else. This document you have created goes well with other tutorials I am doing right now and I have bookmarked it!

    Nostrovia!
     
  12. shweta

    shweta

    Joined:
    Mar 31, 2011
    Posts:
    2
    Hai... i want to develop an app in which i want take a 3d model of a shirt and 3d model of man and in my app i want to fit that shirt to man and change its colour i thought it is possible in unity.. so please guide me regarding this app how should i proceed....
     
  13. sushanta1991

    sushanta1991

    Joined:
    Apr 3, 2011
    Posts:
    305
    I guess i could get this guide when i was new to unity but now i am understanding all unity script. But i appreciate your effort and thanks tonyd because learning have no ends . . . .
     
  14. xxxDjdogxxx

    xxxDjdogxxx

    Joined:
    Mar 28, 2011
    Posts:
    751
  15. heromal

    heromal

    Joined:
    Jan 28, 2011
    Posts:
    14
    I hope you don't mind if I use this as a sort of template for my Boo scripting guide. I'll give credit to you for the idea. I wanted to use the exact scripts you did, so people could read both of our tutorials and see how they compare.
     
    Last edited: Jun 20, 2011
  16. tonyd

    tonyd

    Joined:
    Jun 2, 2009
    Posts:
    1,224
    Sounds great, we could use a similar Boo guide.
     
  17. heromal

    heromal

    Joined:
    Jan 28, 2011
    Posts:
    14
    Thanks, working on it now :D
     
  18. heromal

    heromal

    Joined:
    Jan 28, 2011
    Posts:
    14
  19. DonGato

    DonGato

    Joined:
    Dec 9, 2010
    Posts:
    45
    Quick question here.

    Can you have an array of classes?


    Code (csharp):
    1. class Person{
    2.     var name;
    3.     var career;
    4. }
    5.  
    6. //Create objects of type Person
    7. var john = Person();
    8. john.name = "John Smith";
    9. john.career = "doctor";
    10. Debug.Log(john.name + " is a " + john.career);
    In other words, if I have this code, can I have an array of Persons?


    I am trying to figure out a way to store multiple variables per array item, and this seems like a good way, if it would work.
     
  20. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Code (csharp):
    1. // Array
    2. var people = new Person[10];
    3. people[0] = new Person();
    4. people[0].name = "John Smith";
    5. people[0].career = "doctor";
    6.  
    7. // List
    8. var people = new List.<Person>();
    9. var john = new Person();
    10. john.name = "John Smith";
    11. john.career = "doctor";
    12. people.Add(john);
    --Eric
     
  21. bgivenb

    bgivenb

    Joined:
    Sep 15, 2010
    Posts:
    449
    how is this not a sticky?
     
  22. soul_slayer9

    soul_slayer9

    Joined:
    Jun 29, 2011
    Posts:
    1
    thnx man thats a lot of info and even more helpful since im a total noob to scripting. but i found it kinda hard to understand from the second last code before INHERITANCE.
     
  23. wrm186

    wrm186

    Joined:
    Apr 4, 2010
    Posts:
    661
    Why is scripting sooooooo confusing to me :(

    I mean I know much more now thanks to you! But it still just confuses me so much. I think it might be one of those things you just cant learn (for me anyway) :)
     
  24. simple modeler

    simple modeler

    Joined:
    Jul 24, 2011
    Posts:
    3
    Your post was very helpfull, thanks.
     
  25. Rush-Rage-Games

    Rush-Rage-Games

    Joined:
    Sep 9, 2010
    Posts:
    1,997
    Looks super helpful, thanks!
     
  26. huxley

    huxley

    Joined:
    Apr 27, 2009
    Posts:
    334
    Can we have a noobie guide to C# pretty please?
     
  27. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    There are already lots of those on the web. The reason for this topic is that UnityScript doesn't exist outside Unity.

    --Eric
     
  28. wuesugi

    wuesugi

    Joined:
    Mar 31, 2011
    Posts:
    6
    its a good article ,thank you
     
  29. logana60

    logana60

    Joined:
    Sep 18, 2011
    Posts:
    39
    thanks for posting this but for a person thats never really programmed before it needs to be explained better

    ps. the human brain learns best and fastest if it can relate something it is learning to something it already knows.
     
  30. abbys

    abbys

    Joined:
    Nov 7, 2011
    Posts:
    5
    thanks for this post, it would help everyone especially me.
     
  31. athomield

    athomield

    Joined:
    Aug 31, 2011
    Posts:
    119
    thanks very much dude
     
  32. Disati

    Disati

    Joined:
    Oct 5, 2009
    Posts:
    111
    Thank you so much Tonyd. It's really kind of you to spend time on doing posts like these. The whole community and I appreciates it.
     
  33. xzile40

    xzile40

    Joined:
    Sep 7, 2011
    Posts:
    87
    Lets bring this back up to the front page of Scripting :)

    Great guide, but i'm still having a hard time myself. One day all of this with just fall on me and I will be able to understand it all, but so far I still am having a hard time.

    I'll review this a few dozen more times. Maybe i'll start to understand :)
     
  34. Ryan5280

    Ryan5280

    Joined:
    Mar 1, 2012
    Posts:
    2
    Thanks! This really helped me.
     
  35. asmith2306

    asmith2306

    Joined:
    Mar 14, 2012
    Posts:
    13
    Can you use classes in unity scripting? And if so, how are they applied?
     
  36. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    The first post in this topic covers that.

    --Eric
     
  37. yavadoo

    yavadoo

    Joined:
    Mar 24, 2012
    Posts:
    102
    Thank you very much, very helpful introduction.
     
  38. r-Og

    r-Og

    Joined:
    Apr 19, 2012
    Posts:
    1
    Thanks for this tut. I've looked at loads of different resources in order to help me understand classes properly. You did it it a few short words - "Classes are simply new types (of variable) that YOU create." I'm not sure if this is the "long" or even most accurate explanation of classes, but it did it for me!!:D
     
  39. sherlockturtle

    sherlockturtle

    Joined:
    Jan 23, 2012
    Posts:
    592
    bump? this is realy helpfull This should be stickied in the scripting forum.
     
    Last edited: Jun 11, 2012
  40. ninjaboynaru

    ninjaboynaru

    Joined:
    Jul 13, 2012
    Posts:
    5
    Thank you so much for this guide to scripting. I seriously recommend you guys check out this guide, http://www.unityscript.com/lessons1/basics.php
    It goes over each part if the basics of Unity script step by step and explains why he did what he did in the script. Combine that tutorial with this one and you are set to make a simple game with Unity.
     
  41. Jacksendary

    Jacksendary

    Joined:
    Jan 31, 2012
    Posts:
    408
    you would say STICKY!!! im more a C# programmer but this guide is really awesome and its hard to find good unity scripting guides but this one is over all really good
     
  42. nuverian

    nuverian

    Joined:
    Oct 3, 2011
    Posts:
    2,087
    Fantastic post..thanks a lot
     
  43. okm1123

    okm1123

    Joined:
    Nov 19, 2011
    Posts:
    71
    Couldnt understand the constructor thing DX
     
  44. chelnok

    chelnok

    Joined:
    Jul 2, 2012
    Posts:
    680
    Code (csharp):
    1. class Animal{
    2.     var name;
    3.     function Animal(n : String){ //this is the constructor
    4.         name = n;
    5.         Debug.Log(name + " was born!");
    6.     }
    7.     function JustAnotherFunctionInsideTheClass(){ //this is NOT the constructor.. there can be only one
    8.         name = n;
    9.         Debug.Log(name + " was born!");
    10.     }
    11. }
    Constructor thing is just a function with the same name as Class, in this case Animal. It will be always called when creating instances of a class like:

    cat = Animal("Caddie");

    When you got that line of code function Animal(n : String) the constructor will be executed, but function JustAnotherFunctionInsideTheClass() will be executed only when you call it, like:

    cat.JustAnotherFunctionInsideTheClass()
     
    Last edited: Sep 13, 2012
  45. kyasuchiro

    kyasuchiro

    Joined:
    Oct 20, 2012
    Posts:
    3
    *bumped to youtube for tuts.... didnt learn enough*
    *bumped at other tuts.... still not enough*
    *bumped at your thread..... !!! *

    BRAVO! NOW I UNDERSTAND HOW UNITY'S CODE DIFFERS FROM JS!
    THANKS ALOT! THIS IS WHAT I HAVE BEEN LOOKING FOR!

    *two thumbs up!*
     
  46. enzeart

    enzeart

    Joined:
    Nov 24, 2012
    Posts:
    1
    Thanks for the guide Tony :D!!! <3
     
  47. rohit_garg

    rohit_garg

    Joined:
    Jan 7, 2013
    Posts:
    6
    What a wonderful guide it is! Can you please post me a link for little advanced level?
     
  48. PH-zero

    PH-zero

    Joined:
    Dec 18, 2012
    Posts:
    5
    Really unsefull stuff!

    thanks. : )
     
  49. PavolM

    PavolM

    Joined:
    Sep 4, 2012
    Posts:
    37
    Hi everyone
    maybe my question seems like too silly but what is meaning of dot ? "." how its read by computer i dont understand this thing something like connecting ? or i use it when i want what to do ?

    like if i write
    i dont understand TIme.time what means
    Code (csharp):
    1. var index : int = Time.time * framesPerSecond;
    or

    Code (csharp):
    1.     var aniPlay = GetComponent ( "aniSprite" );
    2.     aniPlay.aniSprite ( 16, 16, 0, 1, 16, 12 );
    I know that it creates variabile aniPlay and assign aniSprite script component from same gameobject as this assigned to but what means aniPlay.aniSprite ? :D im sorry if i cant explain it well sorry for my english, im not from english speaking country if someone can explain it, please do
    thanks :)
     
  50. apomarinov1

    apomarinov1

    Joined:
    Oct 21, 2012
    Posts:
    66
    Thanks for this thread, it's really useful, but what about other stuff that is used in the scripts? For instance @HideInInspector @RequireComponent, I see this is read by the editor but where can we find documentation for these commands?