Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unity3d Unity主机数据阵列问题_Unity3d - Fatal编程技术网

Unity3d Unity主机数据阵列问题

Unity3d Unity主机数据阵列问题,unity3d,Unity3d,所以我的问题是,每当我尝试用按钮调用hostData数组时,它都返回null,但我知道它不是null。我甚至在update()和get when HostData!=null它返回每个帧,但每当我从按钮调用它时,它都会说它为null,我甚至使用if(hostData==null)来验证它。这可能是什么原因造成的 private HostData[] hostData; private bool refreshing = false; private Text joinServerText; pr

所以我的问题是,每当我尝试用按钮调用hostData数组时,它都返回null,但我知道它不是null。我甚至在update()和get when HostData!=null它返回每个帧,但每当我从按钮调用它时,它都会说它为null,我甚至使用if(hostData==null)来验证它。这可能是什么原因造成的

private HostData[] hostData;
private bool refreshing = false;
private Text joinServerText;
private GameObject joinServerButton;
private GameObject startServerButton;
private GameObject refreshServerButton;


void Awake()
{
    joinServerButton = GameObject.Find ("JoinServerButton");
    joinServerText = GameObject.Find("JoinServerText").GetComponent<Text>();
    startServerButton = GameObject.Find ("StartServerButton");
    refreshServerButton = GameObject.Find ("RefreshServerButton");
}

void Start()
{
    joinServerButton.SetActive (false);
}

void StartServer()
{
    bool useNat = !Network.HavePublicAddress();
    Network.InitializeServer(2,25000, !useNat);
    MasterServer.RegisterHost(gameName,"Tutorial Game Name","This is a    tutorial game");
}

void refreshHostList()
{
    MasterServer.RequestHostList(gameName);
    refreshing = true;
}

void Update()
{
    if (refreshing)
    {
        if(MasterServer.PollHostList().Length > 0)
        {
            refreshing = false;
            Debug.Log (MasterServer.PollHostList().Length);
            hostData = MasterServer.PollHostList();
        }
    }

    if(hostData != null)
    {
        joinServerButton.SetActive (true);
        joinServerText.text = hostData[0].gameName;
        Debug.Log ("hostData[0].gameName");
    }

}
//Messages
void OnServerInitialized()
{
    Debug.Log ("Server Initialized!");
}

void OnMasterServerEvent(MasterServerEvent mse)
{
    if(mse == MasterServerEvent.RegistrationSucceeded)
    {
        Debug.Log ("Registered Server");
    }
}

//UI
public void StartSeverButton()
{
    Debug.Log ("Starting Server...");
    StartServer();
}

public void RefreshHostsButton()
{
    Debug.Log ("Refreshing Hosts...");
    refreshHostList();      
}

public void JoinServerButton()
{

    //for(int i=0; i<hostData.Length; i++)
    //{ 
    if (hostData == null)  <--------- My problem
    {
        Debug.Log("hostData is null");
        //Debug.Log (hostData[0].gameName);
        //Network.Connect (hostData[0]);
    }

}
}
private HostData[]HostData;
私有布尔刷新=假;
私密文本;
私有游戏对象joinServerButton;
私有游戏对象StartServer按钮;
私有游戏对象刷新服务器按钮;
无效唤醒()
{
joinServerButton=GameObject.Find(“joinServerButton”);
joinServerText=GameObject.Find(“joinServerText”).GetComponent();
startServerButton=GameObject.Find(“startServerButton”);
refreshServerButton=GameObject.Find(“refreshServerButton”);
}
void Start()
{
joinServerButton.SetActive(false);
}
void StartServer()
{
bool useNat=!Network.HavePublicAddress();
初始化服务器(225000,!useNat);
注册主机(游戏名,“教程游戏名”,“这是一个教程游戏”);
}
无效刷新主机列表()
{
MasterServer.RequestHostList(游戏名称);
刷新=真;
}
无效更新()
{
如果(刷新)
{
if(MasterServer.PollHostList().Length>0)
{
刷新=错误;
Log(MasterServer.PollHostList().Length);
hostData=MasterServer.PollHostList();
}
}
if(主机数据!=null)
{
joinServerButton.SetActive(真);
joinServerText.text=主机数据[0]。游戏名称;
Log(“主机数据[0].gameName”);
}
}
//信息
void OnServerInitialized()
{
Log(“服务器已初始化!”);
}
主服务器事件无效(主服务器事件mse)
{
if(mse==MasterServerEvent.RegistrationSucceed)
{
Log(“注册服务器”);
}
}
//用户界面
public void StartSeverButton()
{
Log(“正在启动服务器…”);
StartServer();
}
公共无效刷新主机按钮()
{
Log(“刷新主机…”);
刷新主机列表();
}
公共无效JoinServerButton()
{

//对于(int i=0;ii),我通常通过替换以下内容来解决神秘的坏值赋值问题:

private HostData[] hostData;
为此:

private HostData[] _hostData;
private HostData[] hostData { get { return _hostData; } set { _hostData=value; Debug.Log("Host data set. Is null?: "+(_hostData == null), this); } }

运行它,您将立即看到变量何时被设置为null。也许您会发现问题并不是您所想的——例如,它可能从未设置过,并且该类实际上有多个实例,而打印主机计数的实例与具有null变量的实例不同。

它没有解决我的问题问题,但它确实验证了,无论出于何种原因,数组没有保留该值。不过,我确实找到了一个解决方案,只需在我调用的button函数中重新分配该值,它似乎没有延迟(这就是我创建刷新变量的原因).我不知道为什么需要重新分配它,但因为它只有一行代码,我可以使用这个“band-aid”公共void JoinServerButton(){hostData=MasterServer.PollHostList();if(hostData!=null){Debug.Log(“hostData为null”);}最后要记住的是,在C#中,数组是一种引用类型。当您进行赋值时,您实际上没有自己的数组。您有一个与库类共享的数组,库类可以在需要时销毁它。我相信这就是正在发生的事情,尽管我对此感到惊讶,因为这似乎是一种不好的做法r使用垃圾收集语言创建一个库来执行此操作。如果您想确定,请使用
列表替换数组。这样,唯一的共享引用将是
主机数据
对象,而不是列表本身。在看到您的帖子后尝试此操作,但出现此错误“错误CS0029:无法将类型
UnityEngine.HostData[]”隐式转换为
System.Collections.Generic.List”“。关于库类销毁值,我打赌您是对的。哦,您还必须用
HostData=new List(MasterServer.PollHostList());