- Home /
Edit Prefab, Instantiate, Access Array in Clone
Hello,
I have a random planet generation system that edits a Sphere called PlanetPrefab with a PlanetProperties script attached to it. The PlanetProperties component of the PlanetPrefab includes an empty Array(javascript array) called resources. The planet generator script, before it instantiates the prefab, edits the resources Array(among many other things) to select three random strings from another Array(another javascript array) called t1resources(inside the planet generator script) and then adds those selected strings to the resources Array inside PlanetProperties component of the PlanetPrefab. However, when I try to access the information stored within the resources Array of every cloned PlanetPrefab, I get an empty Array and there is my problem.
//and here is my code that edits the prefab
var Planet:GameObject;
var t1resources=new Array("Lithium","Helium","Bor","Flor","Alu$$anonymous$$um","Steel","Silicium","Carbon","$$anonymous$$","$$anonymous$$ethane");
function Start () {
for(var m:int=0;m<3;m++)
{
Planet.GetComponent(PlanetProperties).resources[m]=t1resources[Random.Range(0,t1resources.length-1)];
}
Instantiate(Planet,Vector3($$anonymous$$athf.Cos(angle)*radius+0,0,$$anonymous$$athf.Sin(angle)*radius+0),Quaternion.identity);
}
//here is the code inside the PlanetProperties component
var resources=new Array();
//and here is the code that tries to access the array inside the planetproperties component of a cloned object
var thingy=GameObject.Find("Selection Handler").GetComponent(ObjectSelected).selectedObject;
GUI.Label(Rect(Screen.width/2+30,Screen.height-90,40,20),thingy.GetComponent(PlanetProperties).resources.ToString());
Answer by Seth-Bergman · Jul 25, 2013 at 09:31 AM
you may need to set the length of the array first. adding this line may do it:
var resources=new Array();
resources.length = 3;
Ok here's what I did:
//changed the resources Array in PlanetProperties
var resources:String[];
And manually changed the size of the Array attached to the Prefab to 3 in Unity. Now, when I run the game, it changes the elements of the Array in the Prefab but when I try to access the arrays in the clones I get System.String[]
Your answer
Follow this Question
Related Questions
Load Prefabs in Array with Javascript 1 Answer
drawing GUI or Instantiate an object to highlight multiple object 2 Answers
Assign "Colour" to Prefab -1 Answers
Resizeable gameObject array. 1 Answer
What type of Array should I use? 1 Answer