- Home /
How to create an instance of a class stored in another script
Hey there,
What I want to do is store class definitions in a script attached to gameobjectA, then at runtime instantiate these classes from a script attached to gameobjectB. How would I go about this? I'm working in UnityScript by the way.
You need to indicate if 'class definition' is derived from $$anonymous$$onoBehaviour.
Hey Robertbu, I'l try to be clearer. I have a class called Galaxy defined in script called classes.js which is attached to a gameObject, here is the definition:
public class Galaxy{//$$anonymous$$ain gameworld class, creates and stores systems
var syss : Array = [];//stores systems within the galaxy
function Galaxy(){
}
function Init(me : Player,sls:Array){
var i : int = Random.Range(1,5);//number of systems to be created
var x : int = 0;//for iteration
for (x = 0; x < i; x++){ //for number of systems
var id : int = Random.Range(1,1000);//creating a random number as an ID(for debug purposes)
var gal_position : Vector3;//stores system galaxy position
gal_position=new Vector3(Random.Range(0,10),Random.Range(0,10),Random.Range(0,10));//sets galaxy position to random vector3 between 0 and 9
//Debug.Log("CreatingSys:"+gal_position);
var sec : Sys = Sys();//Creates instance of sector
sec.Init(gal_position,me.gal_pos);
this.syss.push(sec);//and adds it to the galaxy's system list
sls.push(sec);
//Debug.Log(x);
}
//Debug.Log(gal_position);
}
}
I want to access this class definition from another script and construct the class from there like:
var gl : Galaxy = classes.Galaxy();
The error im getting is that Galaxy is not a member of classes. Any assistance would be greatly appreciated, thank you!
Hmm apparently it works fine like this:
var gl : Galaxy = Galaxy();
So Galaxy is within the scope of the other script, not sure how this is happening.