Question by
mrrealmpayne · Dec 30, 2017 at 08:01 AM ·
c#playerprefsinputfield
PlayerPrefs returns nothing
This whole script attached to Player. SaveUsername - to Inputfield. All i want is to set the name of player if he/she beats the highscore.
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.IO;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class Player_score : MonoBehaviour {
public Text[] highScores;
public InputField usernameInput;
string newName = "";
public string trueName = "";
int[] highScoreValues;
public bool alreadyName;
private float timeLeft = 120;
public int playerScore = 0;
public int CoinCost = 100;
public GameObject timeLeftUI;
public GameObject playerScoreUI;
void Start()
{
highScoreValues = new int[highScores.Length];
for (int x = 0; x < highScores.Length; x++)
{
highScoreValues[x] = PlayerPrefs.GetInt("highScoreValues" + x);
}
if(alreadyName = true)
{
PlayerPrefs.GetString("saveName");
}
DrawScores();
}
void Update() {
timeLeft -= Time.deltaTime;
timeLeftUI.gameObject.GetComponent<Text>().text = ("Time Left: " + (int)(timeLeft));
playerScoreUI.gameObject.GetComponent<Text>().text = ("Score: " + playerScore);
if (timeLeft < 0.1f)
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
}
public void SaveUsername()
{
alreadyName = true;
newName = PlayerPrefs.GetString("saveName");
PlayerPrefs.Save();
Debug.Log("AND HIS NAME IS" + newName);
}
void OnTriggerEnter2D(Collider2D trig)
{
if (trig.gameObject.name == "EndLevel") {
CountScore();
CheckForHighScore(playerScore);
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
PlayerPrefs.GetInt("Score");
}
else if (trig.gameObject.name == "Coin" || trig.gameObject.name == "Coin (1)" || trig.gameObject.name == "Coin (2)" || trig.gameObject.name == "Coin (3)" || trig.gameObject.name == "Coin (4)" || trig.gameObject.name == "Coin (5)" || trig.gameObject.name == "Coin (6)")
{
playerScore += CoinCost;
Destroy(trig.gameObject);
}
}
void CountScore()
{
playerScore = playerScore + (int)(timeLeft * SceneManager.GetActiveScene().buildIndex * 17);
}
public void CheckForHighScore(int _value)
{
for (int x = 0; x < highScores.Length; x++)
{
if (_value > highScoreValues[x])
{
for (int y = highScores.Length - 1; y > x; y--)
{
highScoreValues[y] = highScoreValues[y - 1];
}
highScoreValues[x] = _value;
DrawScore2();
SaveScores();
break;
}
}
}
void DrawScores()
{
for (int x = 0; x < highScores.Length; x++)
{
highScores[x].text = (highScoreValues[x].ToString());
}
}
void DrawScore2()
{
for (int x = 0; x < highScores.Length; x++)
{
highScores[x].text = (newName + highScoreValues[x].ToString());
}
}
void SaveScores()
{
for (int x = 0; x < highScores.Length; x++)
{
PlayerPrefs.SetInt("highScoreValues" + x, (highScoreValues[x]));
}
}
}
Comment
Your answer
Follow this Question
Related Questions
PlayerPrefs returns nothing 0 Answers
Display PlayerPref in InputField 1 Answer
Saving an image's state 2 Answers
I'm having a problem while jumping an object in Unity 5 0 Answers
PlayerPrefs not saving after restarting level in editor 1 Answer