No Unityscript Object Initializers ??

Can Unityscript use Object initalizers like C# ??

How do you convert this to Unityscript?
Example:

var obj = new Object()
{
    Value1 = 0,
    Value2 = 5
};

Use the Awake() and Start() methods.

When you Instantiate a game object, all it’s Awake() functions are called as part of the Instantiate call. Start() won’t get called until later.

If you need to pass in some references, then we usually have a public void Init(parameters); function that gets called by whatever instantiated the game object.

I don’t think you read the post…
I want to use “Object initalizers” in UnityScript… nothing you said is related to my question…

Sorry man, not game objects got it. I’m assuming you aren’t talking about ScriptableObjects.

I’m used to the term Constructor for what you describe. As you already know, they shouldn’t be used on MonoBehaviours.

Searching “UnityScript constructor” on Google brings this up as the fourth result. Unity has some examples of contructors in UnityScript.
http://unity3d.com/learn/tutorials/modules/beginner/scripting/classes

Searching “JavaScript constructor” brings this StackOverflow answer up as the first result.
http://stackoverflow.com/questions/1114024/constructors-in-javascript-objects

No constructor is not the common term, “Object initializer” is as its a very specific way to initalize something without a constructor (Look at the link I posted).
I am not trying to create GameObjects or MonoBehaviours here. I am creating Desc objects that have no barring on Unity’s Engine.

I dont think that this is possible in UnityScript

Thats rather sad as they are very nice.

Alright man I read it. Sorry I haven’t even heard of this before. Is it just a syntax convenience thing? I don’t want to hijack your thread but this is something I haven’t heard of before so I’m curious to the answer as well.

I’m not a huge JavaScript guy, so maybe someone can explain what’s the difference between these two in C#? From the link zezba9000 provided.

StudentName student4 = new StudentName         
{             
  FirstName = "Craig",             
  LastName = "Playstead",             
  ID = 116         
};

How is this different than that?

StudentName student5 = new StudentName();
student5.FirstName = "Craig";
student5.LastName = "Playstead";
student5.ID = 116;

There is no real difference. Just C# syntactic sugar

Alright cool. I learned something today. =)

Short answer, there is no difference. They simply make writing code faster and nicer to look at.
Object Initializes are very common in C# and C++. I personally hate UnityScript as it is frankly a bad lang to use, but this was not for me.

They can become handy when you only want to initialize one or two vars. because you can write it in one line and still remain readability

var student4 = new StudentName() { FirstName = "Craig", LastName = "Playstead", ID = 116 };

It’s hard to know what US does and does not support, because there’s not comprehensive language reference AFAIK. However in this post I outline many of the expected feature differences. And yes, object initializers are on it.

US does have some feature’s of it’s own of course, but I don’t have a list of them on hand.

I also ran into another issue with it. UnityScript doesn’t even support “ref” parameters. Why is not everyone using C# Idk.
Example: “void FooCallback(ref int value) {…}”