Parameters at Startup

Hey guys,

I’m having a great deal of trouble figuring out how to get parameters on an application’s StartUp. I’ve been working on this almost all day, but nothing seems to work. I can’t give Start() parameters, and I can’t get Environment.GetCommandLineArgs to return any value. Maybe I’m sending the parameters wrong, but I’m getting tired of trying to figure this out. If any of you can give me a quick example, I’ll be really grateful!

Thanks for your time - Gibson

Under OSX I made the following testrun, maybe it helps you to find your issue

Terminal line:

open CMDTest.app/ --args -myarg andstuff and more

MonoBehavior

using UnityEngine;
using System.Collections;
using System;

public class Info : MonoBehaviour 
{	
	static string cmdInfo = "";

	void Start () 
	{
		string[] arguments = Environment.GetCommandLineArgs();
		foreach(string arg in arguments)
		{
			cmdInfo += arg.ToString() + "

";
}
}

	void OnGUI()
	{
		Rect r = new Rect(5,5, 800, 500);
		GUI.Label(r, cmdInfo);
	}
}

Found this very useful thanks!

I implemented your helper as:

class ArgHelper
{
    public static string GetArg(string name)
    {
        var args = System.Environment.GetCommandLineArgs();
        for (int i = 0; i < args.Length; i++)
        {
            if (args[i] == name && args.Length > i + 1)
            {
                return args[i + 1];
            }
        }
        return null;
    }
}

and was able to called it:

TMPtext.text = ArgHelper.GetArg("-name") ?? "nothing";

And from:
powershell
Start-Process -FilePath “.\myapp.exe” -ArgumentList ‘-name ben’

command prompt
start myapp.exe -name ben

OSX terminal
open myapp.app --args -name ben