- Home /
Updating an array indices dynamically
All,
I have a script that records the time and 3D transform positions since game load. I want to end up with an array like this, which records all of the positions and timestamps collected for the positions until it reaches a certain position:
myArray =[
{positionX : #, positionY: # , positionZ : #, timeStamp: #},
{positionX : #, positionY: # , positionZ : #, timeStamp: #},
etc...];
Here is what I've done to take a stab at this, shortened for easier read:
var myObject : GameObject;
var myArray = new Array ();
var seconds;
function Update(){
//stuff here to find seconds, deleted to save space
for (var i=1;i<transformarray.length;i++){
myArray[i]={};
myArray[i].positions=myObject.transform.localPosition; //here, positionX, etc
myArray[i].timeStamp =seconds;
};
if (myObject.transform.localPosition.x = 1 &&myObject.transform.localPosition.z = 3 &&myObject.transform.localPosition.y = 2) {
stop updating array
Debug.Log(myArray);
}
}
Instead, I get
Debug.Log[Object[]);
Could someone help me with this?
Answer by Zodiarc · Mar 13, 2018 at 03:10 PM
I'd rather do an array of specialized objects instead of an arbitrary array of arrays:
public class PositionWithTimestamp {
public Vector3 position;
public int timestamp
public PositionWithTimestamp (Vector3 position, int timestamp) {
this.position = position;
this.timestamp = timestamp;
}
}
then you can fill it dynamically like this:
List<PositionWithTimestamp> positions = new List<PositionsWithTimestamp>();
if(transform.position.x != 1 && transform.position.y != 2 && transform.position.z != 3) { // choose your values as you need
positions.Add(new PositionsWithTimestamp(transform.position, seconds));
}
This way you will be adding your positions as long as you don't stand in that specific position. To stop all adding you could do the following:
bool addPositions = true;
if(transform.position.x != 1 && transform.position.y != 2 && transform.position.z != 3) { // choose your values as you need
addPositions = false;
}
if(addPositions){
positions.Add(new PositionWithTimestamp(transform.position, seconds));
}
All assuming that you're doing it in Update()
Also I'd recommend to switch to C# since UnityScript is deprecated and this will bite you in the a$$ later.
Thank you, I managed to get it to work with this help. Yea I do need to get into C#, I got into UnityScript too deep
Answer by Summit_Peak · Mar 13, 2018 at 12:13 AM
Try using == instead of =
if (myObject.transform.localPosition.x = 1
if (myObject.transform.localPosition.x == 1
Also, try looking at an array element or value instead:
Change:
Debug.Log(myArray);
To:
Debug.Log(myArray[0]);
Debug.Log(myArray[0].timestamp);
@Summit_Peak thank you for your advice. I've tried both, changing = to ==, and also trying to look at the element or value. I'm not sure if it is working, as I still get Object[] as an output when I look at the array itself, or if I look at myArray[0], it says that what I'm looking for is outside the length of the array.
I've added a print(i)
in the for loop, and i
remains at 1 when I take it outside of the Update()
function, and fluctuates between 1 and 2. This could be a hint...I'll play around and update if I can manage to find the answer.