Search Unity

Transform.Position Arrays!

Discussion in 'Scripting' started by Cooper37, Nov 26, 2015.

  1. Cooper37

    Cooper37

    Joined:
    Jul 21, 2012
    Posts:
    383
    Hello! I'm trying to put the position of each Transform into an array, but it won't let me(says some cross between Transforms and Vector3). Do anyone know a way around this?

    js.
    Code (csharp):
    1.  
    2. var savePosition : boolean;
    3. var positions : Transform[];
    4.  
    5. public function Save(){
    6.     if(savePosition){
    7.         for(var i = 0; i < positions.Length; i++){
    8.             data.positions[i] = positions[i].transform;
    9.         }
    10.     }
    11. }
    12.  
    13. public function Load(){
    14. if(savePosition){
    15.             for(var i = 0; i < positions.Length; i++){
    16.                 for(var p : Transform in positions)
    17.                     p.position = data.positions[i];
    18.             }
    19.         }
    20. }
    21.  
     
  2. DeathQuake

    DeathQuake

    Joined:
    Oct 24, 2012
    Posts:
    64
    I am guessing that you are trying to assign your transform to a position ( Vector3) on Line 8.

    Try using
    Code (CSharp):
    1. data.positions[i] =  positions[i].transform.position;
     
  3. Cooper37

    Cooper37

    Joined:
    Jul 21, 2012
    Posts:
    383
    I changed line 17 to this:
    Code (csharp):
    1.  
    2. p.transform.position = data.positions[i].transform.position;
    3.  
    But I still have the error in line 8.
     
  4. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    Last edited: Nov 26, 2015
  5. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    What Type is data.positions? Because if data.positions isn't ALSO an array of Transforms, this isn't going to work.
     
  6. Cooper37

    Cooper37

    Joined:
    Jul 21, 2012
    Posts:
    383
    This is data:
    Code (csharp):
    1.  
    2. var data : GameData = new GameData();
    3.  
    This is used from the persistent data code but in JavaScript. Could I turn this into an array?
     
  7. bayview

    bayview

    Joined:
    Apr 3, 2014
    Posts:
    7
    suggest u change Transform to GameObject
    Code (CSharp):
    1. data.positions[i] = positions[i].transform;
    positions are Transform Type
    it maybe work this way
    Code (CSharp):
    1. data.positions[i].gameobject = positions[i].gameobject;
     
  8. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    What is game data? Paste the class. You're trying to cast a Vector3 to a Transform, so that's the problem.
     
  9. Cooper37

    Cooper37

    Joined:
    Jul 21, 2012
    Posts:
    383
    This is my class as well, it's also a Transform.
    Code (csharp):
    1.  
    2. class GameData(){
    3.    var positions : Transform[];  
    4. }
    5.  
     
  10. Cooper37

    Cooper37

    Joined:
    Jul 21, 2012
    Posts:
    383
    And surprisingly, that didn't work either...
     
  11. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    where do you set the size of your array ? Also changing the number of transform you want to store, you have to rebuild the array because arrays are not resizeable.