Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by aszucs · Jun 06, 2011 at 04:06 PM · standalone

how to detect if there is an instance of my game already running?

player is able to launch the game [standalone executable PC/Mac] multiple number of times.

I should make sure only 1 istance of my game is running at once. I can quit from the game at startup, but i don't know how to detect if an other isntace of my game is already running.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
3

Answer by CinnaMint · Dec 04, 2011 at 08:17 AM

(I know this is old, but here might be an answer. It's not the right answer, but it explains something).

As far as I know, there is no way to accurately deal with this. I myself have needed a solution for this, but both options are marred.

First, Unity 3 Standalone's on Windows platform are -SUPPOSED- to have a command line that you can type in to force the game to only run one instance. It's as follows:

 GameName.exe -single-instance

Theoretically, if the game is started again, it will find the current process and switch focus to it instead. However, I haven't got that command line attribute to work, or any other Windows ones (Such as -popupwindow). So I went on to try something else...

 System.Diagnostics.Process.GetProcessesByName("name");
 System.Diagnostics.Process.GetProcesses();

The System.Diagnostics.Process namespace in .NET contains a lot of nice little goodies. Such as the ability to search currently running processes on a machine for one of a particular name (GetProcessByName), or give a list of all currently running processes (GetProcesses) so you can search if your Game's process is already running, and kill the game before THAT version loads.

However, the Mono version currently included in Unity is actually an older version, which contains a bug. So any use of those two aforementioned functions within System.Diagnostics.Process will give Exception Errors for no reason.

Using GetProcesses() returns an array of currently running processes, but getting anything but the PID of the process returns an Exception error, saying the process has exited (But it hasn't).

Using GetProcessByName() won't go anywhere, either, and gives the same error.

So... In short, I've found out that you cannot check if your game, or anything else is running.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ABTOMAT · May 26 at 01:24 AM 0
Share

Hey! After more that a decade I come to say that "System.Diagnostics.Process will give Exception Errors for no reason" this was fixed in newer versions of Unity and this method works now! Also here's a function to check:

 bool IsAlreadyRunning()
 {
     int count = 0;
     System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcesses();
     foreach (System.Diagnostics.Process process in processes)
     {
         if (process.ProcessName == Consts.processName)
         {
             count++;
             if (count > 1)
             return true;
         }
     }

     return false;
 }

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Vulkan Frame Dropping 0 Answers

Unity standalone takes over 6 secs to start 3 Answers

Regarding PlayerPrefs vrs. (WebPlayer && Standalone) Builds 1 Answer

WWW zero sized post buffer error Standalone 1 Answer

Audio sampling rate higher than 48 kHz? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges