- Home /
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.
Your question is very vague and undetailed, I assume you have to create some button and as an event you add one of these public methods to set the time of the player based on the UI field. But this should all be relatively basic getting some data in unity maybe you should look at some of the tutorials first?
@ShadyProductions Yes, I did look into tutorials. Yet still I can't seem to get it correct because I might miss something from the script. (meaning adding some other scripts and then taking it out because it did not work)
Lets see now so I have the whole score screen builded up. Players can see their time and there is one text field where they put their name innit. After filling this text field they hit continue and will be taken to the scoreboard (which is still in the same scene). Where they should see their name and score while also seeing other peoples scores in neat line. But the name itself is not showing up in the scoreboard neither does the score.
So I wish to see the name and score to be taken to this list. I am still looking into it while asking help on this problem.
Perhaps you should show us how exactly you are trying to set the text into the scoreboard?
Answer by mihoci10 · Mar 06, 2019 at 09:24 AM
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 :)
Your answer
Follow this Question
Related Questions
Terminal-like GUI, wait for input 1 Answer
Send inputfield entries via email 2 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Unity destroy 1 of 2 exactly the same GameObjects with 1 script 1 Answer