C# first script code to run (main function)?

I have years of coding experience but I’m very new to Unity.
Been looking for a Main function to put the code I want to run first.
From what I have read online there is no Main function and I should instead create a initial scene with a object with a component with a script file and in that script file in the start function put my start up code.
But all the answer I found on this where old posts so I want to check if anything has changed in newer versions of Unity?
Or if anyone can link me to some C# examples
Or posts explaining where to put start up scripts.

I have already read the bellow posts on this.

I just want to write a program to take a cmd line argument that is an stl file path. Read the file and load the data into a mesh while displaying a progress bar and then display the model.
I can already read the file and load it to display an object but want to move the code to a startup script and figure out cmd line arguments and how to do a progress bar.

I know. When you build commercial stuff for a while, Unity feels bass-ackward, at first. I assure you, that feeling fades quickly. It’s very different because the domain of video games is very different from what happens in the rest of the software world.

What you think of as the main function is doubtless buried deep in the Unity runtime… like most of the behavior would be for a Windows Forms application. You have to plug your software in through (predominantly) the unity event cycle as applies to behaviors (meaning inheritors of UnityEngine.MonoBehaviour). Those behaviors instances must be attached to game objects, which you configure through the Unity editor in the hierarchy window/pane.

There are other ways to interact with the system but that is the main one.

Here is what I do:

  • Build the model, interactions - basically everything I can about my game - using Visual Studio. I set the assembly with the game code to compile to a subfolder of my Unity project’s Assets folder. By tradition, I use “Assets\bin”. I have no idea if “bin” matters (though I suspect it doesn’t) because I’ve never tried anything else.
  • Build very thin inheritors of MonoBehaviour which exist purely to mediate between what I wrote in Visual Studio (the “real game”) and the Unity object model.
  • Build out an hierarchy of game objects and attach behavior instances to them as I go.
  • Make sure what I did works.
  • Go back to 1 for the next iteration.

You can find plenty of tutorials for most of the parts of that online.

2 Likes

Additional info - Unity’s a c++ engine under the hood. It’s got a C# runtime you write user scripts in, and a C# API you use to interact with the engine.
If you want to run the main yourself, and use the engine as a library, you’ll want a different engine!

The command line arguments you pass to the built .exe are passed to the C# runtime, though, so you should be able to just check Environment.GetCommandLineArgs. There’s probably a million tutorials on how to do a progress bar - you’ll want a relatively recent (less than 2 years old) one that talks about Unity’s “new” (now not so new) UI system, though.

I’ve had the exact question, some data that must be loaded first before any other gameObjects got awaken.
Here is what I’m doing:

  1. Make a Loading scene.
    The scene has nothing, but one gameObject called Main, and it has a script called Main.cs, which has a Awake() method, and which works just like the main methods.
  2. In the Main.Awake(), you can just do your initialization and load the actual entry scene.

Well, loading another scene seems to be a trouble? What I did is adding an abstract layer…

So I did my own scene management,

  1. Base class : abstract BaseScene { Load(); Unload(); OnLoaded(); OnBeforeUnload(); }
  2. UnityScene : BaseScene, which loads the unity scene.
  3. VirtualScene : BaseScene, which is just a prefab, that acts like a scene.

And in my case, I have a loading scene that contains a Main.cs. And a entry scene called MainScene which actually is a UnityScene.
My usage is simple:

  1. (in the loading scene) Main.cs : Awake()
    {
    SceneManager.Instance.RegisterScene(“NameOfTheScene”, “Assets/Res/_Scenes/MainScene.unity”);
    // … do my initialization
    SceneManager.Instance.LoadScene(“NameOfTheScene”);
    }

  2. MainScene.cs
    public class MainScene:UnityScene
    {
    public override void OnLoaded()
    {
    // load other stuff. I believe this is also called before other gameObjects.Awake() in the MainScene.unity.
    }
    }

Then you are good to go…

1 Like