If I have a script called HtMod under a GameObject that has a variable called yCoord, I know I can access it with
gameObject.GetComponent(HtMod).yCoord
Is there a way I can promote that variable so that I can access it via gameObject.yCoord?
Not a problem to work around it, but I was just wondering if there was a way to save all that typing and all those GetComponent() calls…
Cache your script during Awake().
For C#:
private HTMod htmodScript;
private void Awake()
{
htmodScript = gameObject.GetComponent<HTMod>();
}
For UnityScript:
private var htmodScript : HTMod;
private function Awake()
{
htmodScript = gameObject.GetComponent("HTMod");
}
Now, whenever you want to access yCoord, you only have to type htmodScript.yCoord (or whatever you want to name your cache variable as).