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. Dismiss Notice

No Unityscript Object Initializers ??

Discussion in 'Scripting' started by zezba9000, May 28, 2014.

  1. zezba9000

    zezba9000

    Joined:
    Sep 28, 2010
    Posts:
    982
    Last edited: May 29, 2014
  2. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    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.
     
  3. zezba9000

    zezba9000

    Joined:
    Sep 28, 2010
    Posts:
    982
    Last edited: May 28, 2014
  4. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    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
     
    Last edited: May 29, 2014
  5. zezba9000

    zezba9000

    Joined:
    Sep 28, 2010
    Posts:
    982
    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.
     
    Last edited: May 29, 2014
  6. Deleted User

    Deleted User

    Guest

    I dont think that this is possible in UnityScript
     
  7. zezba9000

    zezba9000

    Joined:
    Sep 28, 2010
    Posts:
    982
    Thats rather sad as they are very nice.
     
  8. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    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.
    Code (csharp):
    1.  
    2. StudentName student4 = new StudentName        
    3. {            
    4.   FirstName = "Craig",            
    5.   LastName = "Playstead",            
    6.   ID = 116        
    7. };
    8.  
    How is this different than that?
    Code (csharp):
    1.  
    2. StudentName student5 = new StudentName();
    3. student5.FirstName = "Craig";
    4. student5.LastName = "Playstead";
    5. student5.ID = 116;
    6.  
     
  9. Deleted User

    Deleted User

    Guest

    There is no real difference. Just C# syntactic sugar
     
  10. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    Alright cool. I learned something today. =)
     
  11. zezba9000

    zezba9000

    Joined:
    Sep 28, 2010
    Posts:
    982
    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.
     
  12. Deleted User

    Deleted User

    Guest

    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
    Code (csharp):
    1.  
    2. var student4 = new StudentName() { FirstName = "Craig", LastName = "Playstead", ID = 116 };
    3.  
     
  13. npsf3000

    npsf3000

    Joined:
    Sep 19, 2010
    Posts:
    3,830
  14. zezba9000

    zezba9000

    Joined:
    Sep 28, 2010
    Posts:
    982
    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) {...}"