How do you initialize a transform variable in a class?

How do you initialize a transform variable in the class? (unityscript)

class blueUnit {
var root:Transform; //this needs to be initialized.
var health:int = 0;
var speed:float = 0.0;
}

I know I could do it outside of the class. gameObject.root=gameObject.transform;
How do you do it inside the class?

I guess you can’t? http://forum.unity3d.com/threads/49277-Making-a-new-transform-in-code I was trying to get it to work in conjunction with an array. I just want a direct way to set the class transforms for enemy. But I can’t so I’m making an objectstorage to load it. And then at Start, sending everything in objectStorage over to the enemy[ ].root.

class enemyUnit {
var root:Transform;
var health:int;
var speed:float;
}

var allEnemyHealth:int;
var allEnemySpeed:float;

var enemies:int;
var root:Transform;
var objectStorage:Transform[];

var enemy:enemyUnit[];


function Awake () {
root=transform;

var enemyObject = root.GetComponentsInChildren(Transform);
enemies=((enemyObject.length)-1); //subtracting from 1 because of the parent object to the enemies is being counted in the array along with the children.
objectStorage = new Transform[enemies]; //////////////////////////////// IS OBJECT STORAGE EVEN NECESSARY?!?!? HATE THIS
enemy = new enemyUnit[enemies];

print(enemyObject.Length);
//skip 0
print(enemyObject[0]);


//enemy[0]=enemyObject[0];

for(var n = 1; n < enemyObject.Length; n++){
objectStorage[n-1]=enemyObject[n]; //subtracting one.. because the group object of the enemies is being included ih the enemyObject array.
//enemy[n-1]=enemyObject[n]; 
}


}
function Start() {
for(var b = 0; b < enemies; b++) {
//enemyAI(enemy[b]);
enemy[b].root=objectStorage[b];
enemy[b].health = allEnemyHealth;
enemy[b].speed = allEnemySpeed;
}
}

I am totally lost in your logic… What are you trying to achieve? If you only need to have a list of instances of a component in the scene then what you want to use is FindObjectsOfType in the script where you need it.

If you want to dynamically fill a list of enemies when they spawn then you can better work it with a static List (if “Enemy” is your enemy script) like this for example:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Enemy : MonoBehaviour {
	
	public static List<Enemy> instances = new List<Enemy>();
	
	void Awake () {
		instances.Add(this);
	}
	
	void OnDestroy () {
		instances.Remove(this);
	}
}

Of course you can.

class blueUnit {
    var root:Transform; //this needs to be initialized.
    var health:int = 0;
    var speed:float = 0.0;
}

function Start () {
    var unit = new blueUnit();
    unit.root = transform; // or whatever method you want
}

Though it would be better if you made a constructor for it.

–Eric

I am controlling all of one type of enemy with one script.