Search Unity

static variable

Discussion in 'Scripting' started by MitchStan, Nov 16, 2007.

  1. MitchStan

    MitchStan

    Joined:
    Feb 26, 2007
    Posts:
    568
    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
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    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):
    1. var someScript : ScriptName;
    2.  
    3. function Start() {
    4.      someScript = GetComponent(ScriptName);
    5. }
    6.  
    7. function Whatever() {
    8.      someScript.variable = blah;
    9. }
    --Eric
     
  3. forestjohnson

    forestjohnson

    Joined:
    Oct 1, 2005
    Posts:
    1,370
    The trick is to use GetComponent once, then it is not a speed issue.

    So don't do something like:

    Code (csharp):
    1.  
    2. function Update ()
    3. {
    4.     GetComponent(Foo).variable = "hello";
    5. }
    6.  
    instead do:

    Code (csharp):
    1.  
    2. private var foo : Foo;
    3.  
    4. function Start ()
    5. {
    6.     foo = GetComponent(Foo);
    7. }
    8.  
    9. function Update ()
    10. {
    11.     foo.variable = "hello";
    12. }
    13.  
    (your a fast one eric :) )
     
  4. MitchStan

    MitchStan

    Joined:
    Feb 26, 2007
    Posts:
    568
    Thank you guys - works great - and thank you for teaching me - everytime you guys answer a question, we learn so much. A big thanks.
     
  5. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    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):
    1. var mCamera : Transform;
    2. var mReverse = false;
    3.  
    4. function Start(){
    5. var playa = GameObject.FindGameObjectsWithTag("Player");
    6.    
    7.   if (mCamera == null  GameObject.FindWithTag("TargetCamera"))
    8.                mCamera = GameObject.FindWithTag("TargetCamera").transform;
    9. }
    10. function LateUpdate(){
    11.  
    12.      if (playa.Length <= 0)
    13.          Destroy(gameObject);
    14.     transform.LookAt (mCamera);
    15.  
    16.     if ( mReverse )
    17.         transform.LookAt (transform.position + mCamera.rotation * Vector3.back, mCamera.transform.up);
    18.     else
    19.         transform.LookAt (transform.position + mCamera.rotation * Vector3.forward, mCamera.transform.up);
    20. }
    Thanks
    AC
     
  6. hsparra

    hsparra

    Joined:
    Jul 12, 2005
    Posts:
    750
    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):
    1.  
    2. private var playa : Player;
    3.  
    4. function Start(){
    5.   playa = GameObject.FindGameObjectsWithTag("Player");
    6.    
    7.   if (mCamera == null  GameObject.FindWithTag("TargetCamera"))
    8.                mCamera = GameObject.FindWithTag("TargetCamera").transform;
    9. }
    10. ...
    11.  
    Note that there is no var in front of the reference to playa in the Start() function.
     
  7. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    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