Hey guys, I have this script: Code (csharp): var searchTag = "Enemy"; var scanFrequency = 1.0; static var Target : Transform; function Start() { InvokeRepeating("ScanForTarget", 0, scanFrequency ); } function ScanForTarget() { Target = GetNearestTaggedObject(); } function GetNearestTaggedObject() : Transform { var nearestDistanceSqr = Mathf.Infinity; var taggedGameObjects = GameObject.FindGameObjectsWithTag(searchTag); var nearestObj : Transform = null; for (var obj : GameObject in taggedGameObjects) { var objectPos = obj.transform.position; var distanceSqr = (objectPos - transform.position).sqrMagnitude; if (distanceSqr < nearestDistanceSqr) { nearestObj = obj.transform; nearestDistanceSqr = distanceSqr; } } return nearestObj; } 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): var QA : int; var QB : int; var QC : int; var Wrong; var Descide : int; Descide = Random.Range(1,10); var searchTag = "Enemy"; var Enemy : GameObject; var reachRange : float =10.0; var stringToEdit : String; Wrong = Random.Range(1,114); QA = Random.Range(1,12); QB = Random.Range(1,12); var WinX : int; var WinY : int; var InpX1: int; var InpY1 : int; var InpX2: int; var InpY2 : int; var Player : Transform; var Enemhealth; var nearestEnemy = Target; function OnGUI () { var dist = Vector3.Distance(nearestEnemy.position, transform.position); if(reachRange > dist) { GUI.Box(Rect(WinX,WinY,100,90), QA.ToString()+"x"+QB.ToString()); if(Descide < 5) { if (GUI.Button (Rect (InpX1,InpY1,80,20), QC.ToString())) { Descide = Random.Range(1,10); Wrong = Random.Range(1,114); QA = Random.Range(1,12); QB = Random.Range(1,12); Enemhealth.EnemyHealth -=1; } if (GUI.Button (Rect (InpX2,InpY2,80,20), Wrong.ToString())) { Health.Health -=10; Descide = Random.Range(1,10); Wrong = Random.Range(1,114); QA = Random.Range(1,12); QB = Random.Range(1,12); } } if(Descide > 5) { if (GUI.Button (Rect (InpX1,InpY1,80,20), Wrong.ToString())) { Health.Health -=10; Descide = Random.Range(1,10); Wrong = Random.Range(1,114); QA = Random.Range(1,12); QB = Random.Range(1,12); } if (GUI.Button (Rect (InpX2,InpY2,80,20), QC.ToString())) { Descide = Random.Range(1,10); Wrong = Random.Range(1,114); QA = Random.Range(1,12); QB = Random.Range(1,12); Enemhealth.EnemyHealth -=1; } } } } function Start() { Enemhealth = nearestEnemy.GetComponent(AI2); } function Update() { Enemhealth = nearestEnemy.GetComponent(AI2); QC = QA*QB; if(Wrong == QC) { Wrong = Random.Range(1,114); } if(Descide == 5) { Descide = Random.Range(1,10); } } I have tried just putting in Target, for example: Code (csharp): 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): var nearestEnemy = Target; 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?
put in function Start(). or use in Start() nearestEnemy = GetComponent("YourClass").Target; ex: var Target static: Code (csharp): var nearestEnemy : Transform; function Start() { nearestEnemy = YourClass.Target; } var Target public: Code (csharp): var nearestEnemy : Transform; function Start() { nearestEnemy = GetComponent("YourClass").Target; }
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): 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?
Code (csharp): Classname.Target.position; //Classname = ScriptName where you declared Target Not sure if making it static is a good idea. Are you sure you'll always need just one instance of it?
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.
If it's on the same Gameobject you can use GetComponent(). Just like fabsk8 showed you, only don't use quotes there. Code (csharp): var nearestEnemy: Transform; function Start() { nearestEnemy = GetComponent(TargetingScriptName).Target; }
I tried this: Code (csharp): var nearestEnemy : Transform; function Start() { nearestEnemy = GetComponent(TargetingScriptName).Target; } but when I play the first script has a transform for Target, but the nearestEnemy variable said None(Transform)
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.