I've been accessing the value of a variable in another script by using GetComponent(ScriptName).variable ... it works fine, but reading some threads on GetComponent, I understand GetComponent slows down performance quite a bit. So I read through the manual a bit and learned that it I make the variable type static, I can access it without using GetComponent, simply with -- ScriptName.variable. Works fine, almost - but I have two questions: 1] Is this faster that using GetComponent? 2] I have several GO's (Instantiated from a prefab) so they all have the same ScriptName. How can I use ScriptName.variable so that it points to the correct Script if there's many GO's with the same name? The script that is calling for the variable is attached to a GO that is a child of the GO that has the correct Script to access. So anyway to do a ScriptName.variale from the transform.parent? Again, I'm just trying to avoid GetComponent if that is worh doing. Thanks for any help. Mitch
You can always just cache the GetComponent call if speed is a concern. But unless you're referencing the variable a lot, I doubt it's worth worrying about (i.e., don't optimize prematurely). Code (csharp): var someScript : ScriptName; function Start() { someScript = GetComponent(ScriptName); } function Whatever() { someScript.variable = blah; } --Eric
The trick is to use GetComponent once, then it is not a speed issue. So don't do something like: Code (csharp): function Update () { GetComponent(Foo).variable = "hello"; } instead do: Code (csharp): private var foo : Foo; function Start () { foo = GetComponent(Foo); } function Update () { foo.variable = "hello"; } (your a fast one eric )
Thank you guys - works great - and thank you for teaching me - everytime you guys answer a question, we learn so much. A big thanks.
Please excuse me for bumping a stale thread but I'm a bit stuck, and this conversation almost explains it. In this script, how do I get the "playa" variable defined in Start()to be relevant to the lateupdate function? Code (csharp): var mCamera : Transform; var mReverse = false; function Start(){ var playa = GameObject.FindGameObjectsWithTag("Player"); if (mCamera == null GameObject.FindWithTag("TargetCamera")) mCamera = GameObject.FindWithTag("TargetCamera").transform; } function LateUpdate(){ if (playa.Length <= 0) Destroy(gameObject); transform.LookAt (mCamera); if ( mReverse ) transform.LookAt (transform.position + mCamera.rotation * Vector3.back, mCamera.transform.up); else transform.LookAt (transform.position + mCamera.rotation * Vector3.forward, mCamera.transform.up); } Thanks AC
Define your playa variable outside the Start() function so the other functions know about it. If it is defined in the Start() function then only the Start() function knows about it. If you hear someone referring to scope this is what they are talking about, Code (csharp): private var playa : Player; function Start(){ playa = GameObject.FindGameObjectsWithTag("Player"); if (mCamera == null GameObject.FindWithTag("TargetCamera")) mCamera = GameObject.FindWithTag("TargetCamera").transform; } ... Note that there is no var in front of the reference to playa in the Start() function.
Awesome. I totally get it now. I think this is going to open up a whole heap of possibilities to me now-Thank you Ifrog AaronC