help with lists - reading the last line

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

Hi anseyder ,

your first class must not be a MonoBehavior. Monobehavior scripts have to be placed in a script component on a game object for them to exectue, and you cannot create instances of them with the ‘new’ keyword.

So, when you try to create a new PosTime and add it to the list, it’s failing. Remove the ‘: MonoBehavior’ from the PostTime class declaration, and it will work.

I would have thought the compiler would have generated an error for you when you tried to do that, but maybe not?

Thanks for the answer - I removed the MonoBehavior section and go the ‘PosTime’ is missing the class attribute ‘ExtensionOfNativeClass’! error, however I removed the script from the object in the scene and it works fine now. thanks again!

However, in the Debig.Log I am still only getting: “PosTime”, the name of the class, but not the last entry in the list.

the section where I am trying to get the last string added to the list printed to the debug log is:

//attempt to get last item in list:
        if(positionTimeList.Count>0)
        {
            Debug.Log("index number = " + indexNo);
            Debug.Log("positionTimeList..." + positionTimeList [indexNo]);
            indexNo++;

        }

I made the indexNo an int earlier.
but the section: +positionTimeList[indexNo]); just returns: “PosTime”…which is the name of the class not the last line in the list…?

Any further help on this would be greatly appreciated - thanks!