Need assistance with internal variables

Hello all, first off I am very new to this so please bare with me. I have spent alot of time making this script and finally finished but then I realized that I cannot apply the selected variables to a prefab to instantiate… So now I have to add the variables to the code for spawned enemies and am having issues doing it. Any help would be appreciated. Thanks in advance.

Here is the code, I need both “ammoprefab” and “find” variables added to the code, I have attempted to add the “find” variable but its not working and frankly I just dont know how to go about it…

var ammoprefab: Transform;

private var nextTime : float;

var Delay = 1.5;

var range = 500;

var find : Transform;

function Awake(){

    var find = transform.FindGameObjectsWithTag("Humvee");

}

function Update(){

transform.LookAt(find);

if(Vector3.Distance(transform.position, find.position) < range)

if(Time.time > nextTime){

shoot();

nextTime = Time.time + Delay; }

}

function shoot()

{
	var ammo = 
	Instantiate(ammoprefab,transform.Find("f2").transform.position, transform.rotation);

	ammo.rigidbody.AddForce(transform.forward *10000);

	Destroy(ammo.gameObject, 2); 

}

Hope I explained this right, just for clarity, I need to have these variables placed in the code by default and not a variable I have to add through unity (which is what they are right now). My attempt to do this myself has failed and I feel like I’m just missing something obvious… Thanks again.

Gaspar

Well, there are quite a few issues here, but let’s go one by one.

First of all, your Awake function should probably look like this:

function Awake () {
  find = transform.FindGameObjectWithTag("Humvee");
}

This will only work if there is a single object in your game with the tag of “Humvee”. If there are many of them, it will only find one and assign it to the variable “find”.

Unity docs have an example of finding the closest enemy: Unity - Scripting API: GameObject.FindGameObjectsWithTag

You could use that script almost exactly. I’ve copied it here, with humvee added:

// Find the name of the closest enemy
function FindClosestEnemy () : GameObject {
    // Find all game objects with tag Enemy
    var gos : GameObject[];
    gos = GameObject.FindGameObjectsWithTag("Humvee"); 
    var closest : GameObject; 
    var distance = Mathf.Infinity; 
    var position = transform.position; 
    // Iterate through them and find the closest one
    for (var go : GameObject in gos)  { 
        var diff = (go.transform.position - position);
        var curDistance = diff.sqrMagnitude; 
        if (curDistance < distance) { 
            closest = go; 
            distance = curDistance; 
        } 
    } 
    return closest;    
}

So, I’d put the FindClosestEnemy script into your script file, and this along with it:

var ammoprefab: Transform;
private var nextTime : float = 1.0;
var Delay = 1.5;
var range = 500;

function Start () {
  InvokeRepeating ("Shoot", 0, nextTime);
}

function Shoot () {
  var target = FindClosestEnemy();
  if(Vector3.Distance(transform.position, target.position) < range) {
    transform.LookAt(target.transform);
    var ammo = Instantiate(ammoprefab,transform.Find("f2").transform.position, transform.rotation);
    ammo.rigidbody.AddForce(transform.forward *10000);
  }
}

I haven’t tested this, but let me know if it works.