2D数组返回null

本文关键字:null 返回 数组 2D | 更新日期: 2023-09-27 18:04:00

我正在研究一个项目,该项目需要我访问多个不同的2D数组,用于会话映射。我有两个不同的类,talkinterface是主类,其代码调用另一个会话类busstaionconvo。然而,当我调用它并显示字符串[]时,它返回为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 () {
}
}

任何帮助都将非常感激!

2D数组返回null

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

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