Search Unity

Access static variable?

Discussion in 'Scripting' started by Team TSA, Jun 20, 2012.

  1. Team TSA

    Team TSA

    Joined:
    Oct 19, 2011
    Posts:
    164
    Hey guys,

    I have this script:
    Code (csharp):
    1. var searchTag = "Enemy";
    2. var scanFrequency = 1.0;
    3. static var Target : Transform;
    4.  
    5. function Start() {
    6.     InvokeRepeating("ScanForTarget", 0, scanFrequency );
    7. }
    8.  
    9. function ScanForTarget() {
    10.     Target = GetNearestTaggedObject();
    11.  
    12. }
    13.  
    14. function GetNearestTaggedObject() : Transform {
    15.  
    16.     var nearestDistanceSqr = Mathf.Infinity;
    17.     var taggedGameObjects = GameObject.FindGameObjectsWithTag(searchTag);
    18.     var nearestObj : Transform = null;
    19.     for (var obj : GameObject in taggedGameObjects) {
    20.  
    21.         var objectPos = obj.transform.position;
    22.         var distanceSqr = (objectPos - transform.position).sqrMagnitude;
    23.  
    24.         if (distanceSqr < nearestDistanceSqr) {
    25.             nearestObj = obj.transform;
    26.             nearestDistanceSqr = distanceSqr;
    27.         }
    28.     }
    29.  
    30.     return nearestObj;
    31. }
    That I am using to find the closest object with a certain tag. It works fine, but when I try and access my Target variable in my other scripts, it doesnt recognize it, even though it is static:

    Code (csharp):
    1. var QA : int;
    2. var QB : int;
    3. var QC : int;
    4. var Wrong;
    5. var Descide : int;
    6. Descide = Random.Range(1,10);
    7. var searchTag = "Enemy";
    8. var Enemy : GameObject;
    9. var reachRange : float =10.0;
    10. var stringToEdit : String;
    11. Wrong = Random.Range(1,114);
    12. QA = Random.Range(1,12);
    13. QB = Random.Range(1,12);
    14. var WinX : int;
    15. var WinY : int;
    16. var InpX1: int;
    17. var InpY1 : int;
    18. var InpX2: int;
    19. var InpY2 : int;
    20. var Player : Transform;
    21. var Enemhealth;
    22. var  nearestEnemy = Target;
    23.  
    24. function OnGUI () {
    25.         var dist = Vector3.Distance(nearestEnemy.position, transform.position);
    26.         if(reachRange > dist)
    27.         {
    28.            
    29.             GUI.Box(Rect(WinX,WinY,100,90), QA.ToString()+"x"+QB.ToString());
    30.                 if(Descide < 5)
    31.                 {
    32.  
    33.                     if (GUI.Button (Rect (InpX1,InpY1,80,20), QC.ToString()))
    34.                     {
    35.                         Descide = Random.Range(1,10);  
    36.                         Wrong = Random.Range(1,114);
    37.                         QA = Random.Range(1,12);
    38.                         QB = Random.Range(1,12);
    39.                         Enemhealth.EnemyHealth -=1;
    40.                      }
    41.            
    42.                     if (GUI.Button (Rect (InpX2,InpY2,80,20), Wrong.ToString()))
    43.                     {
    44.                         Health.Health -=10;
    45.                         Descide = Random.Range(1,10);  
    46.                         Wrong = Random.Range(1,114);
    47.                         QA = Random.Range(1,12);
    48.                         QB = Random.Range(1,12);
    49.                     }
    50.                 }  
    51.                    
    52.                 if(Descide > 5)
    53.                 {
    54.                          
    55.                     if (GUI.Button (Rect (InpX1,InpY1,80,20), Wrong.ToString()))
    56.                     {
    57.                         Health.Health -=10;
    58.                         Descide = Random.Range(1,10);  
    59.                         Wrong = Random.Range(1,114);
    60.                         QA = Random.Range(1,12);
    61.                         QB = Random.Range(1,12);
    62.                      }
    63.            
    64.                     if (GUI.Button (Rect (InpX2,InpY2,80,20), QC.ToString()))
    65.                     {
    66.                         Descide = Random.Range(1,10);  
    67.                         Wrong = Random.Range(1,114);
    68.                         QA = Random.Range(1,12);
    69.                         QB = Random.Range(1,12);
    70.                         Enemhealth.EnemyHealth -=1;
    71.                     }
    72.                                    
    73.          
    74.     }
    75.     }
    76. }
    77. function Start() {
    78.     Enemhealth = nearestEnemy.GetComponent(AI2);
    79. }
    80.  
    81. function Update() {
    82. Enemhealth = nearestEnemy.GetComponent(AI2);
    83.    QC = QA*QB;
    84.         if(Wrong == QC)
    85.         {
    86.             Wrong = Random.Range(1,114);
    87.         }  
    88.         if(Descide == 5)
    89.         {
    90.             Descide = Random.Range(1,10);
    91.         }        
    92.     }
    93.  
    I have tried just putting in Target, for example:
    Code (csharp):
    1. var dist = Vector3.Distance(Target.position, transform.position);
    but that doesnt work, it doesnt recognize Target as a variable. I also tried assigning it to another variable, and calling through that:

    Code (csharp):
    1.  
    2. var nearestEnemy = Target;
    3. var dist = Vector3.Distance(nearestEnemy.position, transform.position);
    but it still doesn't recognize Target. I thought static made the variable accessible like any other variable in any other script?
     
  2. fabsk8

    fabsk8

    Joined:
    Jun 6, 2012
    Posts:
    26
    put in function Start().

    or use in Start() nearestEnemy = GetComponent("YourClass").Target;

    ex:

    var Target static:

    Code (csharp):
    1.  
    2. var nearestEnemy : Transform;
    3.  
    4. function Start()
    5. {
    6.     nearestEnemy = YourClass.Target;
    7. }
    8.  
    9.  
    var Target public:

    Code (csharp):
    1.  
    2. var nearestEnemy : Transform;
    3.  
    4. function Start()
    5. {
    6.     nearestEnemy = GetComponent("YourClass").Target;
    7. }
    8.  
    9.  
     
  3. Tanel

    Tanel

    Joined:
    Aug 31, 2011
    Posts:
    508
    You access it with classname.Target. In JS the script name is the class name.
     
  4. Team TSA

    Team TSA

    Joined:
    Oct 19, 2011
    Posts:
    164
    But what happens when I dont have a classname? Target is a transform. I want it to use target like any other variable
    Code (csharp):
    1. var dist = Vector3.Distance(Target.position, transform.position);
    I don't need to access a component of Target ( at least not in this instance)

    How can I get it to work as though the variable was a local one?
     
  5. Tanel

    Tanel

    Joined:
    Aug 31, 2011
    Posts:
    508
    Code (csharp):
    1.  
    2. Classname.Target.position; //Classname = ScriptName where you declared Target
    3.  
    Not sure if making it static is a good idea. Are you sure you'll always need just one instance of it?
     
  6. fabsk8

    fabsk8

    Joined:
    Jun 6, 2012
    Posts:
    26
    put the name of your script in the front

    YourScript.Target.position
     
  7. Team TSA

    Team TSA

    Joined:
    Oct 19, 2011
    Posts:
    164
    Well, when I don't make it static, it almost never seems to work. Both the scripts are on the same gameobject, so Im not sure I can do a GameObject.Find() or something similar to access it if I dont make it static.
     
  8. Tanel

    Tanel

    Joined:
    Aug 31, 2011
    Posts:
    508
    If it's on the same Gameobject you can use GetComponent(). Just like fabsk8 showed you, only don't use quotes there.

    Code (csharp):
    1.  
    2. var nearestEnemy: Transform;
    3. function Start()
    4. {
    5. nearestEnemy = GetComponent(TargetingScriptName).Target;
    6. }
    7.  
     
  9. Team TSA

    Team TSA

    Joined:
    Oct 19, 2011
    Posts:
    164
    I tried this:

    Code (csharp):
    1.  
    2. var nearestEnemy : Transform;
    3. function Start() {
    4.         nearestEnemy = GetComponent(TargetingScriptName).Target;
    5. }
    6.  
    7.  
    but when I play the first script has a transform for Target, but the nearestEnemy variable said None(Transform)
     
    Last edited: Jun 20, 2012
  10. Team TSA

    Team TSA

    Joined:
    Oct 19, 2011
    Posts:
    164
    Nevermind, I fixed it! For anyone having a similar problem, my problem was A, I put the wrong script name in the GetComponent. (I'm so dumb)

    B, I also needed to put it in function update due to the fact that my first script scans for objects every .1, and the second script was looking for it at Start(), so the first script hadnt found it by the time the second one searched for it.