I am trying to instantiate some prefabs into a circle.
I put together this code
var breakCube: Transform;
function Awake () {
for (i=0; i < 360; i++){
var breakCubeClone : Transform[] = Instantiate(breakCube, transform.position, transform.rotation);
breakCubeClone[i].transform.Translate(Vector3.up * 3);
transform.rotation.z = i;
}
}
Now this code doesnt work. But I think it gives the jist of what I’m trying to do.
Insantiate prefab
rotate 1 degree
translate up on the local axis
repeat every degree so that I have a ring of them
How would I fix this code? I couldn’t find any examples anywhere of instantiating prefabs to arrays, so if anyone could point me to one, or show me the code to do this, I’d appreciate it.
for deg in range(0, 360):
rad = deg * Mathf.Deg2Rad
x = transform.position + Mathf.cos(rad)*radius
y = transform.position + Mathf.sin(rad)*radius
cubeClone = Instantiate(breakCube, Vector3(x, y, 0), Quaternion.Euler(0 0, deg))
cubeCloneArray.Append(cubeClone)
One:
Instantiate returns a singular transform, whereas Transform[ ] is an array.
ALso, native arrays do not allow “adding”.
The solution?
NOTE: Untested code:
var arr : Transform[];
var ob : GameObject;
function Start ()
{
//Instantiate five objects, and put them into an array.
var temparr = new Array();//A classic array that can handle multiple types of information, and object "adding".
//As long as the data is of consistant type, this can be applied to a built in array.
for (i = 0; i<5; i++)
{
temparr.Add(Instantiate(ob,transform.position,transform.rotation));
}
arr = temparr;
}
Thank you for the code, it is relatively working, with just a few quirks.
Here is the code fixed up
var cubeDestroy : Transform;
function Awake ()
{
var cubeDestroyArray = new Array();
for ( var i = 0 ; i < 360 ; i++)
{
cubeDestroyArray.Add(Instantiate(cubeDestroy,transform.position,transform.rotation));
cubeDestroyArray[i].rotation.z = i;
cubeDestroyArray[i].transform.Translate(Vector3.up * 3);
}
}
The problem with that code is, this is how it places the cubes
Which is really just boggling my mind right now.
I did a print(i); and the index does print out as 0,1,2,3,4,5 etc. to 359. But my cubes end up with the rotations, 0, 90, and then get progressively smaller until reaching 180.
Now I am explicitly setting the instances rotation.z to i, then translating it on the local axis. But then it comes out like that? Does rotation.z not operate on direct input of degrees?
Also, how would you implement this in your example
arr = temparr;
I tried var CubeArray : GameObject[ ] = cubeDestroyArray;
But I cant make gameobjects like that, and if I use Transform[ ] it gives me a cast type error.
ALSO. How do I get all my cubes to be instantiates into a gameobject so that I can apply combineMeshes?
var cubeDestroy : Transform;
function Awake ()
{
var cubeDestroyArray = new Array();
for ( var i = 0 ; i < 360 ; i++)
{
cubeDestroyArray.Add(Instantiate(cubeDestroy,transform.position,transform.rotation));
cubeDestroyArray[i].rotation = Quaternion.Euler(0, 0, i);
cubeDestroyArray[i].transform.Translate(Vector3.up * 10);
}
}
I also found the array.ToBuiltin();
so nevermind about that as well
But how do I instantiate the cubes into a gameobject to use the mesh combiner script on them all?
Also, is there anyway at all to instantiate straight into a native array?
I’m gonna be instantiating these cubes on the fly in potentially high numbers. It seems like first spawning it into a normal array, then transfering it to a native array would take up unnecessary resources.
var myarray : Transform[];
var ob : GameObject;
//Add each item one at a time, instead of all at the end.
function Start()
{
for (i =0; i < 100; i++)
{
var arr = new Array();
arr = myarray;
clone = Instantiate(ob,transform.position,transform.rotation)
//Do anything you like with clone.
arr.Add(clone);
myarray = arr;
arr = null;
}
}
The only difference is that arr.add, and var arr = new Array are inside the for sequence, instead of outside. Also, in the last example I forgot to add arr =null, which dumps arr. (It will still be dumped if that is the end of Start(), though.) Finally, in order for the add to work, instead of being a replace, arr needs to be up to date. The arr = myarray line was added.
It’s actually much faster to do it the other way, but this is more direct.
Just out of curiosity, do you know if C# or Boo would offer a different way to handle this? Could they read instantiates directly into a native array?
Heres the functioning code if anyone else needs it
var cubeDestroy : Transform;
var distanceApart : int;
function Awake ()
{
var cubeDestroyArray = new Array();
for ( var i = 0 ; i < 360/distanceApart ; i++)
{
cubeDestroyArray.Add(Instantiate(cubeDestroy,transform.position,transform.rotation));
cubeDestroyArray[i].rotation = Quaternion.Euler(0, 0, i * distanceApart);
cubeDestroyArray[i].transform.Translate(Vector3.up * 10);
cubeDestroyArray[i].transform.localScale.x = .5;
}
var builtinDestroyArray : Object[] = cubeDestroyArray.ToBuiltin(Object);
cubeDestroyArray = null;
}
var cubeDestroy : Transform;
var distanceApart : int;
var layers : int;
var planetRadius : int;
function Awake ()
{
var cubeDestroyArray = new Object[(360/distanceApart)*layers];
var instantiateCounter : int = 0;
for (var i2 = 0; i2 < layers; i2++)
{
instantiateAmount = instantiateCounter;
for ( var i = instantiateAmount ; i < (360/distanceApart) + instantiateAmount ; i++)
{
cubeDestroyArray[i] = Instantiate(cubeDestroy,transform.position,transform.rotation);
cubeDestroyArray[i].rotation = Quaternion.Euler(0, 0, i * distanceApart);
cubeDestroyArray[i].transform.Translate(Vector3.up * (planetRadius - i2));
cubeDestroyArray[i].transform.localScale.x = .5;
instantiateCounter++;
}
}
}
Sweet.
And does anyone know how to instantiate these objects as being children of another gameObject? I essentially need this array of hundreds of cubes to move together. Traversing through the entire array with a .translate seems like the incorrect way to do it.