Multiple Game Instances

Hi Everyone,

Quick question: Is there a way to prevent multiple instances of the same game from launching?

Not in general. What is the particular situation where you need to do this?

We make educational games for kids, so our software will probably run on older machines which would suffer a performance loss if multiple instances are running. Sometimes kids can get click happy and accidentally open several instances of the game.

Hello, sorry for resurrecting this thread, but is there any progress on this? I, too, am interested in ensuring one application runs on one computer at a time.

I managed a partial workaround by saving a registry key when the game starts and removing the key when it quits (OnApplicationQuit). Duplicated instances will call Application.Quit when the registry key is detected.

The bad thing is, if the game terminated unnaturally (End Process from Windows Task Manager), the game could no longer be started until the user manually deletes the registry key. In conclusion, I don’t think this is a viable workaround at all.

Are you on windows? They added a command line argument that will allow only one instance to run. The problem is: it’s a command line argument which means you have to have a shortcut to run the program or through the command line. This approach didn’t work for me because the company I work for opens a folder with all of our programs and the user runs the actual executable.

anyway, here’s the arg if you plan to use it: -single-instance. it’s in the documentation: http://unity3d.com/support/documentation/Manual/Command%20Line%20Arguments.html

1 Like

Are you on windows? They added a command line argument that will allow only one instance to run. The problem is: it’s a command line argument which means you have to have a shortcut to run the program or through the command line. This approach didn’t work for me because the company I work for opens a folder with all of our programs and the user runs the actual executable.

anyway, here’s the arg if you plan to use it: -single-instance. it’s in the documentation: http://unity3d.com/support/documentation/Manual/Command%20Line%20Arguments.html

Hi Vogles!

I’ve read that page previously but somehow overlooked that point! :sweat_smile:

Thanks for pointing it out. It is a somewhat sufficient solution. I made the installer add the arg to the Start Menu and Desktop shortcuts (yes I’m on Windows).

The only problem will be if the users run the EXE directly. Not sure how to fix that… is there even a way to force an EXE to always run with the argument without using .lnk shortcuts?

Will update if I make any progress. Thanks again!

Hi!

Managed to work around it by creating a batch file which runs the game executable with the “-single-instance” argument, and then converting the BAT file to an EXE.

Then I set the executable to be hidden and assume the user runs the converted EXE. We can’t do anything if the user runs the hidden game executable though. But at least we can rationalise that the executable was not intended for the user to run manually, and the user does so at his own risk.

That’s not a bad idea. Good Job.

Old thread, but if anyone still has trouble googling this (I know I had) and won’t be happy enough about the command-line solution, there’s this: Unity - Scripting API: PlayerSettings.forceSingleInstance

2 Likes

this is gonna prevent multiple instances of your game

using System.Threading;
using UnityEngine;

public class PreventMultipleInstences : MonoBehaviour
{
private static Mutex mutex;
private static string appGuid;

private void Start()
{
// Check if the appGuid is stored in PlayerPrefs
if (PlayerPrefs.HasKey(“appGuid”))
{
appGuid = PlayerPrefs.GetString(“appGuid”);
}
else
{
// Generate a new appGuid and store it in PlayerPrefs
appGuid = System.Guid.NewGuid().ToString();
PlayerPrefs.SetString(“appGuid”, appGuid);
}

string mutexName = “Global\” + appGuid;
bool createdNew;
mutex = new Mutex(true, mutexName, out createdNew);

if (!createdNew)
{
Debug.LogError(“Instance already running”);
Application.Quit();
return;
}

try
{
// Your code to start the app goes here
}
finally
{
mutex.ReleaseMutex();
}
}

}

1 Like

@hny065 Thanks for the contribution, but please edit your post to contain code tags . It’s just better for everyone that way and the code is properly indented.

Also, keep an eye on the dates and make sure not to resurrect very old threads. In this case, however, I think your contribution is appropriate.

2 Likes