Hi,
I’m having a problem where I “lose” variables during object setup. I’m going nuts here, I can’t understand what happens. Here’s what I do:
environment.cs
class Environment () {
public GameObject go; // set in Inspector, inactive in the beginning
DataController dc = go.GetComponent<DataController>();
dc.Init("shipName");
go.SetActive(true);
}
“Ship” GameObject has a DataController and a AppearanceController
DataController
class DataController () {
string shipName;
Hashtable dataShip;
void Init (string shipName) {
Debug.Log("INITING DataController");
dataShip = someWhereElse.loadDatabase(shipName);
shipName = dataShip[someTestVariable];
Debug.Log(shipName); // prints: "shipName"
}
}
AppearanceController
class AppearanceController () {
void Awake () {
Debug.Log("AWAKE in AppearanceController");
dc = this.GetComponent<DataController>();
Debug.Log(dc.shipName); // prints: null
}
}
How come that between the Init of the DataController and the Awake of the AppearanceController the variable “shipName” get “lost” basically?!
Notice that this is quite random. When I place a second Ship GameObject in the scene (that has some other components additionally, which don’t do anything), it works.