So my code is giving me an IndexOutOfRangeException. Here is the code in question:
var objects = new Transform[10];
var sun : Transform;
//if there is a sun, dont make another. If not, create a sun.
if(sunExists == false){
sun = Instantiate(prefab1, new Vector3(0,0,0), Quaternion.identity);
objects[0] = sun ;
masses[0] = 10* prefab1.localScale.x;
sunExists = true;
}
To my eyes, it seems as if it should have no problem adding an object to the first element in the array, but it refuses. The array masses hasn’t given me a problem yet, but in case you need to know, it is instantiated as such:
var masses = new Array();
The error highlighted by this question is occurring on this line:
If you have changed the objects declaration after running the game at least once, you may be one more victim of the Inspector’s elephant memory: it remembers the first declaration forever, even if you edit the script. The easiest solution is to change the variable declaration to private and click Play: this will make the Inspector forget the old declaration.
private var objects = new Transform[10];
If you need objects to be public, just remove the private keyword and run the game again.
when you instantiate a resource i think i loads as a GameObject
try
var objects : Transform[] = new Transform[10];
var sun :Transform;
if(sunExists == false){
sun = Instantiate(prefab1, new Vector3(0,0,0), Quaternion.identity).transform;
objects[0] = sun;
}
or
var objects : GameObject = new GameObject[10];
var sun : GameObject;
if(sunExists == false){
sun = Instantiate(prefab1, new Vector3(0,0,0), Quaternion.identity);
objects[0] = sun;
}