This is actually a continuation from another question I've made before and I'm sorry I couldn't find this in the documentation. I believe this must be there somewhere. What I did found was some other answers on similar subject (coincidentally, a lot of them from SisterKy), but I couldn't abstract them to work in this instance.
I have a script, called GroundCheck.js (can be seen on the other question).
Then I have other scripts calling it.
To each script in which I want to use it, I always use the exact same lines in the function Start to initialize its value, after declaring / instantiating it on global. Simplifying it here:
var groundCheck : GroundCheck = null;
function Start () {
if (groundCheck == null) // This is in case the groundCheck isn't choosen within Inspector, as it should
groundCheck = GetComponentInChildren(GroundCheck);
if (groundCheck == null) // This is in case there is no Component in Children, just to avoid warning messages for debugging purposes
groundCheck = gameObject.AddComponent(GroundCheck);
}
Note this will have different results for running in different objects, and that's just the way this initialization should work. It's just a safe-back and automated way to associate an object to the variable.
I'm hoping to be able to write this initialization just in one place (probably within GroundCheck.js I suppose) rather than writing it repeated in every script where it's being instantiated like I'm doing now.
Hopefully using some kind of "OnConstruction" over GroundCheck.js or something to be able to declare it along the lines of:
Like spree mentioned, if you use have another script component that uses this GroundCheck component, you could let the depending component require the GroundCheck component using RequireComponent. This way, whenever you add a script component that uses the isGrounded variable in GroundCheck, the GroundCheck script component will automatically be added.
Okay, so we're talking about the 4 lines of code you currently have in your Start method of (I assume) multiple different scripts, and the problem is that you have to repeat those 4 lines of code in many different scripts, like in say ScriptA and ScriptB. Is that correctly understood?
You can't have the initialization of ScriptA and ScriptB being done by GroundCheck unless GroundCheck knows about ScriptA and ScriptB. In order for GroundCheck to be able to handle ScriptA and ScriptB using the same code, ScriptA and ScriptB need to either inherit from a common class (like GroundCheckUser) or implement an interface (like IGroundCheckUser).
You could then make GroundCheck search for components that is of the type of that class or interface, and perform the necessary initialization on those components.
If you go for making a common parent class, you could also have the initialization inside that class.
Another method which does not require interfaces or parent classes is to make a function that performs the initialization, so you can cut your 4 lines down to one line (the function call). This function could for example be a static function in GroundCheck.
Thanks to Rune's answer I got to this solution, for GroundCheck.js. This is it at its full:
#pragma strict
@System.NonSerialized
var isGrounded : boolean = false;
function OnTriggerStay () {isGrounded = true;}
function OnTriggerExit () {isGrounded = false;}
function OnCollisionStay () {isGrounded = true;}
function OnCollisionExit () {isGrounded = false;}
static function start (groundCheck : GroundCheck
, getComponentInChildren : System.Object
, transformFind : Transform
, gameObject : GameObject
) : GroundCheck {
if (groundCheck == null) // This is in case the groundCheck isn't choosen within Inspector, as it should
groundCheck = getComponentInChildren;//GetComponentInChildren(GroundCheck);
if (groundCheck == null) {
if (transformFind != null)
groundCheck = transformFind.GetComponent(GroundCheck);
}
if (groundCheck == null) // This is in case there is no Component in Children, just to avoid warning messages for debugging purposes
groundCheck = gameObject.AddComponent(GroundCheck);
return groundCheck;
}
With that I don't get the line the way I wanted, but at least I get to do the initialization on Script A, B, C, etc, in just one (big bloated) line and the instantiation:
var groundCheck : GroundCheck;
function Start () {
groundCheck = GroundCheck.start(groundCheck, GetComponentInChildren(GroundCheck), transform.Find("GroundCheckCollider"), gameObject);
}