Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by watchoutfree · Oct 10, 2012 at 02:17 AM · instantiateprefabarraysreset

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);
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image watchoutfree · Oct 11, 2012 at 04:47 AM 0
Share

i did what you told me but i`m still stuck in the step i think.... ill add the code i have above.

avatar image watchoutfree · Oct 11, 2012 at 04:50 AM 0
Share

it tells me i cannot convert transform[,] into transform...

avatar image
0

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));
  
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fafase · Oct 11, 2012 at 06:00 AM 0
Share

$$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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges