Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Some difficulties to understand UnityScript

Discussion in 'Scripting' started by nio-kasgami, Dec 12, 2015.

  1. nio-kasgami

    nio-kasgami

    Joined:
    Dec 12, 2015
    Posts:
    6
    Hi guys!
    My name is Nio Kasgami and I am somewhat a not so bad coder.
    Although before I was more a Ruby Native and such.
    I mostly experimented JavaScript with Rpg Maker MV.

    So let's going to the point.

    I was curious How do I start a new JS file in my Unity game?
    for each function do I have to create a new file or I can call multiple "function" in the same's file?

    Also the overall structure of how Unity handle JS confuse me a little, but I will being honest I am not really familliar to JavaScript 6 and mostly to ES5.

    Also I have the bad behavior since I am a Ruby native to implement everything's in a classes structure...
    Anyway's I was asking if this actually possible to do this ?

    Code (CSharp):
    1. // Ho My Maid! Base
    2. function HMM_Base(){this.initialize.apply(this,arguments);}
    3.   HMM_Base.prototype.constructor = HMM_Base;
    4.  
    5.  
    6. Object.defineProperties(HMM_Base.prototype, {
    7. // Define maid stamina
    8.     stamina: {get: function(){ return this._stamina; } }, configurable: true },
    9. // Define maid strenght.
    10.     strengh: { get: function() { return this._strengh; }, configurable: true },
    11. // Define maid speed.
    12.     speed: { get: function(){return this._speed; }, configurable: true },
    13. // Define maid dexterity.
    14.    dexterity: {get: function(){return this._dexterity;}, configurable: true },
    15. // Define maid inteligence
    16.    inteligence: { get: function(){return this._inteligence;}, configurable: true },
    17. });
    18.   HMM_Base.prototype.initialize = function() {
    19.     this.clear();
    20.   };
    21.  
    22.   HMM_Base.prototype.clear = function() {
    23.     this._honor   = 0;
    24.     this._strengh = 0;
    25.     this._hunger  = 0;
    26.     this._energy  = 0;
    27.     this._courage = 0;
    28.     this._speed   = 0;
    29.     this._dexterity = 0;
    30.     this._inteligence = 0;
    31.     this._weakPoint = null;
    32.     this._strenghPoint = null;
    33.   };
    34.  
    35. var $maid = new HMM_Base // for make global
    36.  

    As you can see I use prototype...
    SO I do know I don't have to use constructor or this gonna make incompatibility with how Unity work's but do I can work like that? or this impossible?

    thanks for any further answer :)
    Nio Kasgami
     
    Last edited: Dec 12, 2015
  2. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,739
    Forget anything you know about JavaScript, since in unity it does not work like real JavaScript at all. Aside from that o would recommend just go the C# route since its a really language and use useful to learn for more than just Unity.
     
    nio-kasgami likes this.
  3. nio-kasgami

    nio-kasgami

    Joined:
    Dec 12, 2015
    Posts:
    6
    I see but I have to admit actually I find C# really "verbose" I mean it's really having a lot's of syntax and it's kinda scare me a little lol...
    Although if example I want to being able to store "data" like in JS in JSON.
    What's would be the file for store data?
    YAML? XML?
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You are almost on your own with UnityScript. There are a couple of veterans of the forums that still use it. And it can do everything C# can. But there is no specific documentation on the ins and outs of the languages.

    However for specifics
    • UnityScript is a .Net language. Meaning everything is a object/class. If you don't explicitly provide a class the compiler will subclass it from MonoBehaviour for you.
    • You can't explicitly create constructors for MonoBehaviours. Use Awake and Start instead
    • You can produce classes that are not MonoBehaviors. You can do whatever you like here.
    • You have access to (almost) the full .Ner library. So save data in whatever format you like.
     
    nio-kasgami likes this.
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    If you know ActionScript 3, then you should be able to use Unityscript easily. Javascript, not so much.

    --Eric
     
    nio-kasgami likes this.
  6. nio-kasgami

    nio-kasgami

    Joined:
    Dec 12, 2015
    Posts:
    6
    THanks for your answer guys,

    So I am better to Stay with C# and learn it?
    happily Ruby have some base from C so it's not so weird to look at the code from C# even all those "public" "private" "void" is currently confusing me a lot.

    for the MonoBehaviour of what I understand it's the "Super classes" of everything's?

    and also I am interested a lots about the Sprite classes (showing image or bust in game ect)
    Although it's not seem to get named "Sprite"