Hi All, I am VERY new to programming and struggling through as best as I can…
I am creating a car driving game for university where the position, time and (in the future) other input data will be stored in a list. This list is updated several times per second and at the end of the game (10-15 minutes) I need to output the entire list to a csv file. I already have .csv output working in another script, my issue is that I am struggling with the creation and checking of the list. I am trying to print the last item in the list to the debug console so I can be sure that the list is actually working, at the moment it is just returning “null”… I suspect I have set up the list wrong but I am not getting errors. Please can you help?
I have created a script that defines the List (as per the unity list tutorial):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PosTime : MonoBehaviour
{
public string PosX;
public string PosY;
public string PosZ;
public string timeString;
public PosTime(string newPositionX, string newPositionY, string newPositionZ, string newTime)
{
PosX = newPositionX;
PosY = newPositionY;
PosZ = newPositionZ;
timeString = newTime;
}
}
then in another script I am trying to use this to write the XYZ position and Time to the list:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
public class PlayerPositionandInput : MonoBehaviour
{
// the aim is to record the time and position as strings, then store them in list(s). At the end of the game out put the entire list as a
//CSV. Eventually I will also add input details from the steering wheel to record steering input and peddle input too.
[SerializeField] private GameObject PlayerPositionObject;
[SerializeField] private float recordingTimer; //to adjust timing for recording details
//-------------------------------------------------------
// location data
private Vector3 playerPos;
private float playerPosX;
private float playerPosY;
private float playerPosZ;
private string PosX;
private string PosY;
private string PosZ;
//-------------------------------------------------------
//TimeData
private int hour;
private int minute;
private int second;
private int millisecond;
private string TimeString;
//-------------------------------------------------------
//list
List<PosTime> positionTimeList = new List<PosTime>();
private int listCounter;
private string stringCheck;
// Start is called before the first frame update
void Start()
{
InvokeRepeating("UpdateTimeandLocationList", 0.5f, recordingTimer);
}
// Update is called once per frame
void Update()
{
}
void UpdateTimeandLocationList()
{
//Data recording section
//---------------------------------------------------------
//player position
playerPosX = PlayerPositionObject.transform.position.x;
playerPosY = PlayerPositionObject.transform.position.y;
playerPosZ = PlayerPositionObject.transform.position.z;
//position to string
PosX = playerPosX.ToString();
PosY = playerPosY.ToString();
PosZ = playerPosZ.ToString();
//current time
hour = System.DateTime.Now.Hour;
minute = System.DateTime.Now.Minute;
second = System.DateTime.Now.Second;
millisecond = System.DateTime.Now.Millisecond;
TimeString = hour.ToString() + ":" + minute.ToString() + ":" + second.ToString() + ":" + millisecond.ToString();
//----------------------------------------------------------
//Debug.Log("position of player is: " + playerPosX + " , " + playerPosY + " , " + playerPosZ);
//Debug.Log("Time is: " + TimeString);
//----------------------------------------------------
//Add data to the list
positionTimeList.Add(new PosTime(PosX, PosY, PosZ, TimeString));
//attempt to get last item in list:
if(positionTimeList.Count>0)
{
var stringtTest = positionTimeList.Last();
Debug.Log("positionTimeList..." + stringtTest);
}
}
}
Please be mindful that I am really new to programming and still learning basic concepts. Any help as to whether this should work and specifically how I can see/print the last item in the list so I can be sure it is working would be appreciated.
cheers