Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9193773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:14:40+00:00 2026-06-17T21:14:40+00:00

So I would like to make an object that has a bunch of static

  • 0

So I would like to make an object that has a bunch of static methods to it. The methods are an API to a remote server. I was reading and thought I could use the unity StartCoroutine method but thats not available in this situation, so now I don’t know where to go.

The general idea is I want to be able to call one of my object’s methods, pass it a delegate and have id go off an do its work. When its done, call the delegate with the results. I can’t use threading as Unity3D is not thread safe.

I know c# has this yield thing and I’ve read about it several places and it still confuses me. How can I refactor my code below so I can accomplish what I’m attempting to do?

public class Server
{
        private static string baseURL = "http://localhost/game.php";
        private static Hashtable session_ident = new Hashtable();

        //--- Public API
        public delegate void DeviceSeenCallback(bool seen);
        public static void DeviceSeen(DeviceSeenCallback callBack) {
                StartCoroutine(DoDeviceSeen(callBack));
        }

        public delegate void AuthenticateCallback(bool authenticated, string errorMessage);
        public static void Authenticate(string username, string passwordHash, AuthenticateCallback callBack) {
                StartCoroutine(DoAuthenticate(username, passwordHash, callBack));
        }


        //--- Private API
        private static IEnumerator DoDeviceSeen(DeviceSeenCallback callBack)
        {
                WWWForm form = new WWWForm();
                form.AddField("deviceID", SystemInfo.deviceUniqueIdentifier);

                WWW www = new WWW(baseURL + "?cms=seen", form.data, session_ident);
                        yield return www;

                // Check for errors
                callBack(ResultIsOk(www.text));
        }

        private static IEnumerator DoAuthenticate(string username, string passwordHash, AuthenticateCallback callBack)
        {
                WWWForm form = new WWWForm();
                form.AddField("deviceID", SystemInfo.deviceUniqueIdentifier);
                form.AddField("deviceType", SystemInfo.deviceType.ToString() + "||" + SystemInfo.deviceModel);
                form.AddField("user", username);
                form.AddField("pass", passwordHash);

                WWW www = new WWW(baseURL + "?cms=auth", form.data, session_ident);
                        yield return www;

                if (ResultIsOk(www.text)) {
                        callBack(true, "");
                } else {
                        int code;
                        string message;
                        ResultGetError(www.text, code, message);
                        callBack(false, message);
                }
        }

        private static bool ResultIsOk(string resultText) {
                return false;
        }

        private static void ResultGetError(string resultText, out int code, out string message) {
                code = -1;
                message = "Some Error Message";
        }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T21:14:41+00:00Added an answer on June 17, 2026 at 9:14 pm

    The trick you always run into with Unity, is that the only context under which your code runs is during script callbacks.

    This basically means that eventually somewhere, you must have a GameObject in a scene with a MonoBehaviour attached to said object that calls the code you want to run repeatedly.

    Aside from the fact that there is no StartCoroutine available other than in the instance of a class inheriting from MonoBehaviour, you have the problem that you need to have code execute every frame or so to check on something. There’s no real way around this other than by making a MonoBehaviour, just embrace it.

    A common pattern is to create a singleton-esque GameObject that either has DontDestroyOnLoad called on it, or is recreated as needed for each scene. You could even designate one object as a “coroutine bucket” that isn’t tied to a specific class or anything. Literally an empty MonoBehaviour whose sole purpose in life is to start coroutines.

    You can quickly solve this problem by making your Server class a MonoBehaviour:

    And then by adding a singleton property:

        private static Server ServerObject {
            get { 
                if (_serverObject == null) {
                    var gameObj = new GameObject("Server Object");
                    _serverObject = gameObj.AddComponent<Server>();
                    GameObject.DontDestroyOnLoad(_serverObject); // Optional
                }
                return _serverObject;
            }
        }
        private static Server _serverObject;
    

    (Singletons are gross, but it’s a necessary evil in this circumstance)

    And then by changing all your StartCoroutine calls to be called on the singleton MonoBehaviour instance:

        public static void DeviceSeen(DeviceSeenCallback callBack) {
                this.ServerObject.StartCoroutine(DoDeviceSeen(callBack));
        }
    

    GoKit uses this pattern and makes a single GameObject named “Go” that processes all the logic for tweens. Since Unity doesn’t provide a way to specify scripts that run regularly without being attached to a game object, this is the best we can do.

    If your Server class would maintain a lot of state, or it’s not essential that it remain part of th escene, it may be advisable that you don’t call DontDestroyOnLoad on it. If the scene changes and the ServerObject game object is deleted, subsequent checks to _serverObject will come up as null and thus be reinstantiated (since Unity overloads the null conversion operator).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to make an Object that has key names taken from a
I would like to make a child class that has a method of the
I would like to make every EditText object have own title like in Pure
Would like to make anapplication in Java that will not automatically parse parameters used
I would like to make a function that takes in a list of integers
How can I implement a class/object that has a reference to a bunch of
I would like to make a css transition on an element which has display:
I need to make a Javascript object that would behave as an associative array,
I have a javascript object that I would like to update with data from
I would like make a script using PHP (probably need JS) to send POST

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.