Reputation: 309
I am working on a project that requires me to access multiple different 2D array, for conversation mapping. I have two different classes, talkinterface is the main class with the code that calls the other, conversation class busstaionconvo. However, when I call it and display the string[], it is returned as Null. Can anyone help me with this problem? I am writing in C# in MonoDevelop, using Unity game engine, Code is below.
The main class talkinterface partial code:
...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
}
}
The second class busstationconvo entire code:
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 () {
}
}
Any help would be greatly appreciated!
Upvotes: 2
Views: 151
Reputation: 6361
Monobehaviors in Unity run through Unity's own initialization scheme - it isn't reliable to use constructors or static methods to populate data into them as Unity is inflating the object and hooking up the relationships that have been populated in the Unity inspector view. Initialization that you want done in code needs to be triggered in the Start()
function (much as the comment says).
You are accessing the code through a static method on the first object, so that will likely be run before Unity has run Start on the second behavior.
Upvotes: 1