How to add input field into this?

Hello,

I have been working on this for some time. I came into one nice piece of script that help me make a good list for the fastest time players have. But the person who did the script did not have game ready or example how to use it exactly if one has the results of the game and input fields that gives the name of the player.

I laready have done some tweaking on it but I still need to change this a bit so I can use the input fields I have in my game. I have been struggling with it for some time now.

So what do I need to change, in the script, so it will give the results that the player typed down?

So here is the script

using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
using System.Linq;

public class YeTime : MonoBehaviour
{
    Dictionary<string, Dictionary<string, int>> allTimes;

    public Text yourTimeText;

    void Start()
    {
        YourTime.text = "Fastest Time : " + (int)PlayerPrefs.GetFloat("Time_Here");
    }

    private void Init()
    {
        if (allTimes != null)
            return;

        allTimes = new Dictionary<string, Dictionary<string, int>>();
    }

    public int GetTime(string user, string yetime)
    {
        Init();

        if allTimes.ContainsKey(user) == false)
        {
            return 0;
        }

        if (allTimes[user].ContainsKey(yetime) == false)
        {
            return 0;
        }
        return allTimes[user][yetime];
    }

    public void SetTime(string user, string yetime, int arvo)
    {
        Init();
        if (allTimes.ContainsKey(user) == false) {
            allTimes[user] = new Dictionary<string, int>();
        }

       allTimes[user][yetime] = arvo;
    }

    public void ChangeTime(string user, string yetime, int amount)
    {
        Init();
        int nowTime = GetTime(user, yetime);
        SetTime(user, yetime, nowTime + amount);
    }

    public string[] GetPlayerNames(string arrangeByTime)
    {
        Init();

        return allTimes.Keys.OrderBy(n => GetTime(n, arrangeByTime)).ToArray();
    }
}

Thank you in advance.

You could alternatively make a list of structs, which are basically objects, that can have many properties on them, such as ID and name and time. You need to define the struct outside of your class and then just make a list. For example:

public class YeTime : MonoBehaviour{
    List<Record> records = new List<Record>();

    // YOUR CODE HERE //

    void addToRecords(string playerName, float playerTime){
        records.Add(new Record(playerName,playerTime));
    }
}

public struct Record{
    public string name;
    public float time;

    public Record(string name,float time){
        this.name = name;
        this.time = time;
    }
}

Keep in mind I wrote this out of my head, so everything might not work off the bat.
Hope it helps :slight_smile: