In my current project, my scripts often reference other scripts, forcing me to have this at the start of each script:
private var objManager : Obj_Manager;
private var globalManager : Global_Manager;
private var knowledgeManager : Knowledge_Manager;
private var scriptWorld : Script_World;
private var phoneManager : Phone_Manager;
private var worldInteractions : World_Interactions;
private var sfxManager : SFX_Manager;
function Awake (){
scriptWorld = GetComponent(Script_World);
objManager = GetComponent(Obj_Manager);
globalManager = GetComponent(Global_Manager);
knowledgeManager = GetComponent(Knowledge_Manager);
worldInteractions = GetComponent(World_Interactions);
phoneManager = GetComponent(Phone_Manager);
sfxManager = GetComponent(SFX_Manager);
}
As the project gets more complex I wonder, is there a way to simplify this? Is this the normal workflow, or could I have some parent script that references all scripts and they get the references from there?
I would guess you always have one instance of a manager running? So you could do under you var declarations something like:
private GameObject whatever;
public static MyManager myManager;
public static MyManager Instance () {
if (!myManager) {
myManager = FindObjectOfType (typeof(MyManager)) as MyManager;
if (!myManager)
Debug.LogError ("There needs to be one active MyManager script on a GameObject in your scene.");
}
return myManager;
}
In other scripts where you want to use the instance, put in the void Start() method just a: