- Home /
Question by
Coban12346 · Aug 05, 2019 at 12:36 AM ·
playerprefssave datasaveload
PlayerPrefs always return 0 but health variable is 5,I have a problem PlayerPrefs. It always return zero when it starts. But I assign variable 5
using System.Collections; using System.Collections.Generic; using UnityEngine; using System.Linq; using UnityEngine.UI; using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour { public Question[] questions; public Text[] TextofChos;
public Text facttext;
private static List<Question>unanswered;
private Question CurrentQuestion;
public bool itistru = true;
public float health = 5f;
void Start()
{
if (unanswered == null || unanswered.Count == 0)
{
unanswered = questions.ToList<Question>();
}
GetRandomQuestion();
}
void GetRandomQuestion()
{
//privatehealth = starthealth;
health = PlayerPrefs.GetFloat("Health");
int randomQuestionIndex = Random.Range(0, unanswered.Count);
CurrentQuestion = unanswered[randomQuestionIndex];
facttext.text = CurrentQuestion.fact;
for(int i= 0; i<=3; i++)
{
TextofChos[i].text = CurrentQuestion.choisestext[i];
}
}
IEnumerator TransitionToNextQuestion()
{
unanswered.Remove(CurrentQuestion);
yield return new WaitForSeconds(0);
PlayerPrefs.SetFloat("Health", health);
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
health = PlayerPrefs.GetFloat("Health");
Debug.Log(health);
}
// Update is called once per frame
void Update()
{
}
public void A()
{
if(CurrentQuestion.TrueAnswer == 'A')
{
Debug.Log("A");
}
else
{
health -= 1;
Debug.Log("Wrong");
}
StartCoroutine(TransitionToNextQuestion());
}
public void B()
{
if (CurrentQuestion.TrueAnswer == 'B')
{
Debug.Log("B");
}
else
{
health -= 1;
Debug.Log("Wrong");
}
StartCoroutine(TransitionToNextQuestion());
}
public void C()
{
if (CurrentQuestion.TrueAnswer == 'C')
{
Debug.Log("C");
StartCoroutine(TransitionToNextQuestion());
}
else
{
health -= 1;
Debug.Log("Wrong");
}
StartCoroutine(TransitionToNextQuestion());
}
public void D()
{
if (CurrentQuestion.TrueAnswer == 'D')
{
Debug.Log("D");
}
else
{
health -= 1;
Debug.Log("Wrong");
}
StartCoroutine(TransitionToNextQuestion());
}
}
Comment