2D数组返回null

时间:2013-03-15 14:54:27

标签: c# arrays null unity3d

我正在开发一个项目,要求我访问多个不同的2D数组,以进行对话映射。我有两个不同的类, talkinterface 是主要类,其代码调用另一个,会话类 busstaionconvo 。但是,当我调用它并显示字符串[]时,它将返回Null。任何人都可以帮我解决这个问题吗?我在MonoDevelop中用C#编写,使用Unity游戏引擎,Code在下面。

主要类 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 () {

}

}

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

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

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