Search Unity

when i shoud use "void Main" the function be call after awake and before start

Discussion in 'Scripting' started by HX_LSC, Aug 3, 2017.

  1. HX_LSC

    HX_LSC

    Joined:
    Oct 24, 2014
    Posts:
    31
    when i shoud use "void Main" the function be call after awake and before start
    upload_2017-8-3_9-18-52.png
     
  2. HX_LSC

    HX_LSC

    Joined:
    Oct 24, 2014
    Posts:
    31
    it's really interestion, the Main like C# Main function , notice the color between "Main" and "Start"
     
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    6,702
    the order is not based on the order you write them in the class, but rather the order unity calls them.

    With that said, I'm unaware of a message named 'Main' in Unity. In what way are you calling 'Main'?
     
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    6,702
    Ok, I just tested, and if you have a 'Main' method in a MonoBehaviour it is called. And it's sequenced between Awake and Start.

    Not sure what it's for, it appears to be undocumented...

    Anyone know what it's for, it should be avoided (since it is undocumented). Never really called a method Main so I never ran into this before.

    I will repeat though... ordering of your functions in your class does NOT control the order in which Unity sends those messages. The order is what Unity defined it as. And Awake occurs before Start.

    The reason Awake and Start are coloured differently is because the IDE is aware of the Unity messages like Awake and Start and colours them for you appropriately to designate their significance. Since 'Main' is undocumented, it's not considered a message name, and therefore isn't coloured by the IDE.
     
    Last edited: Aug 3, 2017
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    4,249
    Whoa yeah, definitely listen to @lordofduct and avoid this function. While that is NEAT and all, just use either Awake() or Start(), as appropriate. Main() is an undocumented magic function, and it could in the future NOT get called, or get called multiple times, or get called only on certain platforms and not others. AVOID! FEAR! FIRE! FOES! :)
     
    larku and Kiwasi like this.
  6. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,487
    And lets not forget that calling a method Main can also do other nasty things if you ever pull parts of your project back to regular C# land.

    I would strongly advise never to have a method called Main in a Unity project.
     
  7. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    4,286