- Home /
Making a reset function for arrayed objects
Hi, i'm new at this but i'll try to do my best:
I've got several arrays of transforms(8 arrays of 30, all instantiated from one prefab) which i modify with several function -moving their vertices, rotations, position, rendering, etc-.
i wanna make a RESET function in order to make all return to their original state (their state just after their initial instantiation). i tried to store the vertices original position, but without success,
Is there any way to say, go back to the prefab-just after cloned state?
// edit! i dont wanna reset all at the same time, so i was trying to make a function(transform, array) and call it whenever i want.
thanks
function reset (valor:Transform,grupo:Array)
{
for (valor in grupo)
{
var mesh = valor.GetComponent(MeshFilter).mesh;
var groupvertex = mesh.vertices;
for (var i=0;i<groupvertex.Length;i++)
{
Vector3.Lerp(Vector3(groupvertex[i].x,groupvertex[i].y,groupvertex[i].z),Vector3(startPoint[i].x,startPoint[i].y,startPoint[i].z), 1);
}
}
Answer by fafase · Oct 10, 2012 at 11:34 AM
What about creating two arrays at first and using only one. Then when trying to reset use the original one.
The point is your second array is not the same as the first one. Classes and their instantiations are passed by reference, meaning that you modify the object, the reference is modified as well.
var myArray = new GameObject[5];
var myStorage = new GameObject[5];
for (var i:int =0;i<5;i++){
var go = Instantiate(parameters);
myArray[i] = go;
myStorage[i] = go;
}
Both arrays refered to the same object so in the end myStorage is the same as myArray.
What you need is a simple container storing the values you want to keep. You could create a class that holds the values and creates an array of that class.
class info{
var health:int;
var size:int;
var position:Vector3;
//...
}
var myArray = new GameObject[5];
var myStorage = new Info[5];
for (var i:int =0;i<5;i++){
var go = Instantiate(parameters);
myArray[i] = go;
myStorage[i].health = go.health;
myStorage[i].position = go.position;
// ...so on
}
i did what you told me but i`m still stuck in the step i think.... ill add the code i have above.
it tells me i cannot convert transform[,] into transform...
Answer by watchoutfree · Oct 11, 2012 at 05:15 AM
function CrearOctogono (ancho:int,largo:int,a:int)
{
abajo = new GameObject [ancho,largo];
dchabajo = new GameObject [ancho,largo];
izqdabajo= new GameObject [ancho,largo];
izqd = new GameObject [ancho,largo];
dcha = new GameObject [ancho,largo];
dchaarriba= new GameObject [ancho,largo];
izqdarriba= new GameObject [ancho,largo];
arriba = new GameObject [ancho,largo];
for (var x = 0;x < ancho; x++) {
for (var z = 0;z < largo; z++)
{
abajo[x,z] =Instantiate (prefab, Vector3(x*a,0,z*a), Quaternion.identity);
dchabajo[x,z] = Instantiate (prefab, Vector3(a/1.4142+a*2+x*a/1.4142,a/3+a*x/1.4142,z*a), Quaternion.Euler(0,0,45));
izqdabajo[x,z] = Instantiate (prefab, Vector3(-a/1.4142-x*a/1.4142,a/3+a*x/1.4142,z*a), Quaternion.Euler(0,0,-45));
izqd[x,z] = Instantiate (prefab, Vector3(-a*2/1.4142-a,a*3.5/1.4142+a*x,z*a), Quaternion.Euler(0,0,-90));
dcha[x,z] = Instantiate (prefab, Vector3(a*2/1.4142+3*a,a*3.5/1.4142+a*x,z*a), Quaternion.Euler(0,0,90));
dchaarriba[x,z] = Instantiate (prefab, Vector3(a/1.4142+a*2+x*a/1.4142,a*2+a*6.5/1.4142-a*x/1.4142,z*a), Quaternion.Euler(0,0,135));
izqdarriba[x,z] = Instantiate (prefab, Vector3(-a/1.4142-x*a/1.4142,a*2+a*6.5/1.4142-a*x/1.4142,z*a), Quaternion.Euler(0,0,-135));
arriba[x,z] = Instantiate (prefab, Vector3(x*a,a*2+a*7/1.4142,z*a), Quaternion.identity.Euler(0,0,180));
$$anonymous$$mm, You would considerably simplified your code using a dictionary. A dictionary is like an array but ins$$anonymous$$d of having an index and a value, it has a key and a value. The key can be anything like a string. In the end, you would have a dictionary with keys, abajo, dchabajo and so on, and each of them would have the object as value. http://msdn.microsoft.com/en-us/library/xfhwa508.aspx
The point being that you would only have one line of declaration and a dictionary has O(1) for access.
So to use the original idea, create a game object that will have all the info. Then use a dictionary and somehow:
Dictionary<string, StorageObject> myDictionary =
new Dictionary<string, StorageObject>();
StorageObject obj = new StorageObject(the values);
myDictionary.Add("Abajo",obj);
I am not 100% confident with dictionaries so you might have to tweak this a little.
Your StorageObject would have a constructor and all variables.
Finally, avoid posting comment for answers. That is not how it works. I would recommend to remove the answers you posted. At the moment your questions shows 4 answers, so people think you have been helped enough. Just a tip.
Your answer
Follow this Question
Related Questions
Prefabs instantiated from an array are keeping their public int value 1 Answer
Having multiple objects fire prefabs in different times C# 0 Answers
SOLVED : Adding and retrieving instantiated objects from Array 1 Answer
Instantiate issues 1 Answer
ReplacePrefab() resets transforms for instanced prefabs? 1 Answer