0
\$\begingroup\$

This question already has an answer here:

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

public class AttemptCounter : MonoBehaviour {

    public Text AttemptText;
    public int Attempts = 1;
    public int AddAttempts = 1;

    public void AddAttempt()
    {
        Attempts = Attempts + AddAttempts;
        AttemptText.text = ("Attempts: " + Attempts.ToString(""));
        Attempts = PlayerPrefs.GetInt ("Attempts");
        PlayerPrefs.GetInt ("Attempts");
    }

    void Update()
    {
        AttemptText.text = ("Attempts: " + Attempts.ToString(""));
        PlayerPrefs.SetInt ("Attempts", Attempts);
    }
}

I Am Trying To Add a Attempts System In My Game, Every time a user will restart a level the game will add one attempt, however because when restarting the level it uses the code

SceneManager.LoadScene (SceneManager.GetActiveScene ().name);

It doesnt save the int meaning that its keeping the attempt counter on 1, can anyone help, Thanks.

\$\endgroup\$

marked as duplicate by Gnemlock, DMGregory Nov 20 '17 at 3:02

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1
\$\begingroup\$

Well, you are using the lines of code in the wrong order, since you are first setting Attempts without first loading the value stored on the "Attempts" key.

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

public class AttemptCounter : MonoBehaviour
{

    public Text AttemptText;
    public int Attempts = 1;
    public int AddAttempts = 1;

    public void AddAttempt()
    {
        /// 1.- You should first load the saved "Atempsts" value.
        Attempts = PlayerPrefs.GetInt ("Attempts");
        /// 2.- Then you add the attempts.
        Attempts += AddAttempts;
        /// Now the debug should throw you the expected value.
        AttemptText.text = ("Attempts: " + Attempts.ToString(""));
        /// 3.- You don't need to Save the int on update, just when you add it.
        PlayerPrefs.SetInt ("Attempts", Attempts);
    }

}

Now, about other ways of storing data, you could use ScriptableObjects, XML Serialization, or use persistent GameObjects, like this:

public class AttemptsHolder : MonoBehaviour
{
    private int attempts;

    void Awake()
    {
        attempts = PlayerPrefs.GetInt("Attempts");
        DontDestroyOnLoad(gameObject); /// With this, the GameObject doesn't go to the Garbage collector when a scene loads.
    }

    public void SetAttempts(int _attempts){ attempts = _attempts; }
    public int GetAttempts(){ return attempts; }

    public void SaveAttempts()
    {
        PlayerPrefs.SetInt("Attempts", attempts);
    }
}

Hope it helps.

\$\endgroup\$
  • \$\begingroup\$ Thanks For Your Help, Helped a Ton due to me being newish to c# However, Where Would I Put The AttemptsHolder Script In My Game? Attempts text, create a object and add script? \$\endgroup\$ – BreeZy Studios Nov 19 '17 at 22:18
  • \$\begingroup\$ P.S Im Getting error CS0127: AttemptsHolder.GetAttempts()': A return keyword must not be followed by any expression when method returns void \$\endgroup\$ – BreeZy Studios Nov 19 '17 at 22:33
  • \$\begingroup\$ It could be to your Game GameObject, or to an empty one, it depends if you want them to be separated. Sorry that function must return int, my bad. It is now edited. \$\endgroup\$ – LifGwaethrakindo Nov 19 '17 at 22:35
  • \$\begingroup\$ I managed to sort out the function, Thanks for the help however for some unusual reason the text isnt being updated to what it says in the code, I have managed to link everything up, I have set the AddAttempt To be activated on click of a button. \$\endgroup\$ – BreeZy Studios Nov 19 '17 at 22:45
  • \$\begingroup\$ I am seeing why, since I copy-pasted and re-arranged your code, I didn't see that you were using an overload of ToString() giving "" as parameter, you can just append the int to the text, either with String.Append(); or oin this case deleting the ToString(""); call. \$\endgroup\$ – LifGwaethrakindo Nov 20 '17 at 0:56

Not the answer you're looking for? Browse other questions tagged or ask your own question.