Search Unity

I want to add to my total score once per level

Discussion in 'Scripting' started by Pohja, Mar 20, 2019.

  1. Pohja

    Pohja

    Joined:
    Jul 18, 2018
    Posts:
    11
    I want to call a function from another script that adds +1 to my JSON stored int, but I only want to call it once per level and I want to save that I have gotten a "Star" from that one level.

    Code (CSharp):
    1. public void FillList()
    2.     {
    3.         foreach (var level in LevelList)
    4.         {
    5.             GameObject newButton = Instantiate(levelButton) as GameObject;
    6.             LevelButton button = newButton.GetComponent<LevelButton>();
    7.             button.LevelText.text = level.LevelText;
    8.  
    9.             if (PlayerPrefs.GetInt("Level" + button.LevelText.text) == 1)
    10.             {
    11.                 level.Unlocked = 1;
    12.                 level.IsInteractable = true;
    13.             }
    14.  
    15.             button.unlocked = level.Unlocked;
    16.             button.GetComponent<Button>().interactable = level.IsInteractable;
    17.             button.GetComponent<Button>().onClick.AddListener(() => LoadLevel("Level" + button.LevelText.text));
    18.  
    19.             if (PlayerPrefs.GetFloat("Level" + button.LevelText.text + "_score") > 10)
    20.             {
    21.                 button.Star1.SetActive(true);
    22.                 svm.AddStars(); // THIS ADDS ONE STAR
    23.             }
    24.  
    25.             if (level.Unlocked == 0)
    26.             {
    27.                 newButton.GetComponent<Image>().sprite = lockSprite;
    28.  
    29.                 newButton.GetComponentInChildren<Text>().text = "Locked";
    30.             }
    31.  
    32.             newButton.transform.SetParent(Spacer);
    33.         }
    34.         SaveAll();
    35.     }
    Code (CSharp):
    1. void SaveAll()
    2.     {
    3.         if (PlayerPrefs.HasKey("Level1"))
    4.         {
    5.             return;
    6.         }
    7.         else
    8.         {
    9.             GameObject[] allButtons = GameObject.FindGameObjectsWithTag("LevelButton");
    10.             foreach (GameObject buttons in allButtons)
    11.             {
    12.                 LevelButton button = buttons.GetComponent<LevelButton>();
    13.                 PlayerPrefs.SetInt("Level" + button.LevelText.text, button.unlocked);
    14.             }
    15.         }
    16.     }
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You've said what you want, but you haven't described a problem or asked a question.