C# 使用静态函数启动例程从服务器获取时间

C# 使用静态函数启动例程从服务器获取时间,c#,unity3d,static,C#,Unity3d,Static,就像标题上说的,我在争取服务器时间。我知道关于这个问题还有很多其他的问题,但是没有一个对我有帮助。代码如下: using UnityEngine; using System.Collections; public class ServerTime : MonoBehaviour{ public static ServerTime time; void Awake(){ time = this; } public static void Ge

就像标题上说的,我在争取服务器时间。我知道关于这个问题还有很多其他的问题,但是没有一个对我有帮助。代码如下:

using UnityEngine;
using System.Collections;

public class ServerTime : MonoBehaviour{

    public static ServerTime time;

    void Awake(){
        time = this;
    }

    public static void Get(){
        time.StartCoroutine (time.ServerRequest ());
    }

    IEnumerator ServerRequest(){
        WWW www = new WWW ("http://www.businesssecret.com/something/servertime.php");

        yield return www;

        Debug.Log(www.text);
    }
}
我从其他脚本中这样称呼它:

ServerTime.Get();
这应该可以从服务器上打印时间,但我会一直得到

第13行“对象引用未设置为对象实例”:


我做错了什么?请不要将其标记为重复,因为我尝试了从谷歌找到的所有方法,但没有任何帮助。

代码应该按原样工作

您获得该异常的可能原因:

1。您忘记将
ServerTime
附加到空的游戏对象。您必须将其连接到游戏对象,才能运行其
Awake()
函数。调用
Awake()
时,将初始化
time
变量

2。您将
ServerTime
附加到GameObject,但后来它被销毁了

3。统一中的脚本执行顺序问题

调用
ServerTime.Get()的脚本ServerTime
脚本之前加载“代码>自”。这是统一中的一个已知问题,但你可以改变它

下面的代码应修复上述所有3问题。如果调用
ServerTime.Get()
time
变量为
null
,则会创建一个新的游戏对象,并将
ServerTime
脚本附加到该对象上。然后手动初始化
时间

public class ServerTime : MonoBehaviour
{
    private static ServerTime localInstance;
    public static ServerTime time { get { return localInstance; } }


    private void Awake()
    {
        if (localInstance != null && localInstance != this)
        {
            Destroy(this.gameObject);
        }
        else
        {
            localInstance = this;
        }
    }

    public static void Get()
    {
        if (time == null)
        {
            Debug.Log("Script not attached to anything");
            GameObject obj = new GameObject("TimeHolder");
            localInstance = obj.AddComponent<ServerTime>();
            Debug.Log("Automatically Attached Script to a GameObject");
        }
        time.StartCoroutine(time.ServerRequest());
    }

    IEnumerator ServerRequest()
    {
        WWW www = new WWW("http://www.businesssecret.com/something/servertime.php");

        yield return www;

        Debug.Log(www.text);
    }
}
公共类ServerTime:MonoBehavior
{
专用静态服务器时间定位;
公共静态服务器时间{get{return localInstance;}}
私人空间
{
if(localInstance!=null&&localInstance!=this)
{
摧毁(这个游戏对象);
}
其他的
{
LocationInstance=这个;
}
}
公共静态void Get()
{
if(time==null)
{
Log(“脚本未附加到任何内容”);
GameObject obj=新游戏对象(“计时器”);
localInstance=obj.AddComponent();
Log(“自动附加到游戏对象的脚本”);
}
time.start例程(time.ServerRequest());
}
IEnumerator服务器请求()
{
WWW=新WWW(“http://www.businesssecret.com/something/servertime.php");
收益率;
Debug.Log(www.text);
}
}

您的脚本是否在某处被引用?看看Unity Singleton:使用Singleton模式不是更容易、更可重用吗?:@Ludovic Singleton实际上不是问题所在。拥有
ServerTime
的多级实例不会导致该问题。这就是为什么我没有提到它。虽然,我的答案中包含了一个简单的单例,但这不是问题所在。这很可能是我列出的三件事情之一。事实上,我没有将脚本附加到gameObject。我不知道我需要这么做才能醒着跑步。非常感谢。无需检查
localInstance!=这是
Awake()中的
public class ServerTime : MonoBehaviour
{
    private static ServerTime localInstance;
    public static ServerTime time { get { return localInstance; } }


    private void Awake()
    {
        if (localInstance != null && localInstance != this)
        {
            Destroy(this.gameObject);
        }
        else
        {
            localInstance = this;
        }
    }

    public static void Get()
    {
        if (time == null)
        {
            Debug.Log("Script not attached to anything");
            GameObject obj = new GameObject("TimeHolder");
            localInstance = obj.AddComponent<ServerTime>();
            Debug.Log("Automatically Attached Script to a GameObject");
        }
        time.StartCoroutine(time.ServerRequest());
    }

    IEnumerator ServerRequest()
    {
        WWW www = new WWW("http://www.businesssecret.com/something/servertime.php");

        yield return www;

        Debug.Log(www.text);
    }
}