gpt4 book ai didi

c# - 返回为 null 的二维数组

转载 作者:太空宇宙 更新时间:2023-11-03 13:45:39 24 4
gpt4 key购买 nike

我正在从事一个项目,该项目要求我访问多个不同的二维数组,以进行对话映射。我有两个不同的类,talkinterface 是主类,带有调用另一个的代码, session 类 busstaionconvo。但是,当我调用它并显示 string[] 时,它返回为 Null。谁能帮我解决这个问题?我在 MonoDevelop 中用 C# 编写,使用 Unity 游戏引擎,代码如下。

主类talkinterface部分代码:

...public class talkinterface : MonoBehaviour {



....ai = new string[,]{
{"",""}
};



public static void eOption(bool eval, string response){
if(response == "bus"){
Debug.Log (ai); //DISPLAYS string[]
responses = busstationconvo.responses;
ai = busstationconvo.ai;
Debug.Log (busstationconvo.responses); //DISPLAYS null
Debug.Log (ai); //DISPLAYS null
}
}

第二类busstationconvo完整代码:

using UnityEngine;
using System.Collections;

public class busstationconvo : MonoBehaviour {
public static string[,] ai;
public static string[,] responses;
// Use this for initialization
void Start () {
ai = new string[,]{
{"Hola, bienvenido al estacion del autobus." , "0"},
{"Estoy bien y tu?", "1"},
{"Esta es el estation de autobuses.","2"},
{"Que necesitas?","3"},
{"Si, tengo un boleto, cuesta dos dolares.","4"},
{"Para usar el autobus, necesitas un boleto.","5"},
{"Gracias, aqui esta tu boleto.","6"}

};
responses = new string[,]{
//HOLA 0
{"Hola, como estas? ","1"},
{"Que es este lugar?","2"},
{"Necesito ayuda por favor.","3"},
{"Adios.","999"},
//ESTOY BIEN Y TU? 1
{"Estoy bien, adios ","999"},
{"Bien, pero que es este lugar?","2"},
{"Bien pero, necesito ayuda por favor.","3"},
{"Adios.","999"},
//THIS IS THE BUS STATION 2
{"Claro, adios.","999"},
{"Gracias, pero necesito ayuda por favor","3"},
{"Adios.","999"},
{"","2"},
//WHAT HELP DO YOU NEED 3
{"Nada, adios.","999"},
{"Necesito un boleto.", "4"},
{"Necesito un autobus.","5"},
{"Adios.","999"},
//IT COSTS 2 DOLLARS 4
{"Que caro, no gracias.","999"},
{"Que ganga! Tengo dos dolares.", "6"},
{"Por su puesto, tengo dos dolares.","6"},
{"Adios.","999"},
//YOU NEED A TICKET 5
{"Tienes un boleto?","4"},
{"","5"},
{"","5"},
{"","5"},
//HERE’S YOUR TICKET 6
{"Gracias, adios.","999"},
{"","6"},
{"","6"},
{"","6"}
};
}

// Update is called once per frame
void Update () {

}

}

如有任何帮助,我们将不胜感激!

最佳答案

Unity 中的单一行为通过 Unity 自己的初始化方案运行 - 使用构造函数或静态方法将数据填充到它们中是不可靠的,因为 Unity 正在膨胀对象并连接已在 Unity 检查器 View 中填充的关系。您希望在代码中完成的初始化需要在 Start() 函数中触发(正如评论所说)。

您正在通过第一个对象上的静态方法访问代码,因此它可能会在 Unity 对第二个行为运行 Start 之前运行。

关于c# - 返回为 null 的二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15435487/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com