How to cleanly reference other scripts inside a script?
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?
Answer by LE0NIDAS · Apr 25, 2016 at 07:57 AM
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:
myManagerScript = MyManager.Instance ();
of course this is a c# example, you will have to use c# or see how to rewrite it to javascript, but you should get the idea behind it with this snippet.
Answer by Overcast · Apr 25, 2016 at 12:47 PM
Have a manager game object in your scene with a script called Manager, also add your other 'manager' classes like an audio manager.
In that class use this:
public static AudioManager audio;
void Awake()
{
audio = GetComponent<Audio>();
}
Now you can reference your audio manager from anywhere by writing, Manager.Audio.SomeMethod()
Does this solution cause any performance issues? Some of my scripts only call one or two other scripts and they would now be referencing the 'mega' parent script rather than just accessing two of them. Not to mention that this $$anonymous$$anager. would now be available to any other script.
Answer by haraldk · Apr 25, 2016 at 02:45 PM
Utilize a framework like StrangeIoc to keep all your structure and refrences clean
http://strangeioc.github.io/strangeioc/
You can than use dependency injection to get references
Your answer
Follow this Question
Related Questions
component not detecting parent class 0 Answers
New UI - Referencing deep nested components on an prefab instantiation 1 Answer
Component refrence goes null randomly? 3 Answers
How to pass reference to variable in IEnumerator method in C#? 2 Answers
Compute Shader Pass RWStructuredBuffer by Reference 1 Answer