- Home /
What is the best method to have a list of references to other scripts?
I am working in JS and having issues trying to find a way to provide some sort of reference list or class to organize / consolidate all scripts in a project.
Right now I have a way of accessing the "required" scripts in a singleton manager class, these are scripts I know the type of and know they will only be on one gameObject in the scene:
class CManager extends MonoBehaviour {
private static var Instance : CManager;
public static function GetInstance() : CManager {
if (Instance == null){ Instance = FindObjectOfType(CManager); }
if (Instance == null){ Debug.LogError("Can't Find CManager script on the Scene"); return null;
}else{ return Instance; }
}
public class Reference {
public var Reference1 : Reference1;
public var Reference2 : Reference2;
public var Reference3 : Reference3;
public var RefObj1 : GameObject;
public var RefObj2 : GameObject;
}
public static function GetReference(refObject : System.Type) : Object{
return Instance.reference.GetType().GetField(refObject.ToString()).GetValue(Instance.reference);
}
public static function GetReference(refObject : String) : Object{
return Instance.reference.GetType().GetField(refObject.ToString()).GetValue(Instance.reference);
}
And if I wanted to grab references from another script I would use something like:
control = CManager.GetInstance();
ref1 = control.GetReference(Reference1) as Reference1;
ref2 = control.GetReference("RefObj2") as GameObject;
What I am stuck on is the optional references, what I am looking for is a way to have a reference class or list or something to be able to keep track of an unknown amount scripts of an unknown type. (Also need to have these available in the editor environment)
I have tried:
Class -> Issue is I don't know the amount of references or their type
ArrayList -> I am able to add to the list (with different types), but it cant be serializable (that I know of) so I end up losing the list
Generic List (List.< Object > ) -> Works, but same issue as ArrayList...
Are there any solutions or direction to point me in for this? or am I doing this all wrong?