- Home /
Question by
Amon · Apr 16, 2014 at 08:50 PM ·
javascriptlistsinstanceclasses
UNITYSCRIPT - Class's and Instances - Functions/Methods - Correct Usage
Say I have a Class:
//.js Script file named UFO
class UFOs {
public static var ufoList:List.<UFOs>;
var ufoGameObject:GameObject;
var xlocation:float;
var ylocation:float;
public function UFOs( go:GameObject, xl:float, yl:float ) {
ufoGameObject = go;
xlocation = xl;
ylocation = yl;
}
public function RandomMoves() {
random movement code here.;
}
}
============================
//.js Script file named UFOSpawner
var ufo:GameObject; // This is assigned the GameObject via the inspector
function start() {
if ( UFO.ufoList == null ) {
UFO.ufoList = new List.<UFOs>();
}
}
function update() {
if ( key(H) is tapped ) {
var u:UFOs = new UFOs( Instantiate(ufo), transform.position.x, transform.position.y);
UFOs.ufoList.Add(u);
}
if ( UFO.ufoList != null ) {
for ( u in UFOs.ufoList ) {
u.RandomMoves();
}
}
=============================================================================
Am I getting this? The problem I have is when I iterate the list and try to get u.RandomMoves for each u, whether there are 5 or 100 instances only 1 u moves randomly. What am I missing?
regards,
Comment
Your answer
Follow this Question
Related Questions
Where to use Structs and classes? 5 Answers
Trouble with Add-ing and Retrieving from a List of Custom Class 1 Answer
Why does my class script not recognize variables in its own instance? 1 Answer
Cannot get variable from another class. Error: NullReferenceException 2 Answers
Add Item To A List Of Classes 0 Answers