Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Creating level Buttons and Assigning stars

Discussion in 'Game Design' started by Shakira_Mohan, Jan 4, 2022.

  1. Shakira_Mohan

    Shakira_Mohan

    Joined:
    Jan 4, 2022
    Posts:
    1
    I'm working on a game and I'm facing a problem at level selection. I have script for creating level buttons it was working fine unless I'd added a new mode every thing is fine the problem is when I show stars on level button stars from 2nd level of simple mode gets equal to the first level of classic mode for example I have 2 stars on 2nd level of simple mode then the stars of first level of classic mode becomes equal to that. Can any one help me with it
    Code
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using TMPro;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6. using UnityEngine.SceneManagement;
    7.  
    8. public class LevelManager : MonoBehaviour
    9. {
    10. public GameObject loadingPanel;
    11.  
    12. public int numberOfSimpleLevels;
    13. public int numberOfClassicLevels;
    14.  
    15. public GameObject levelButton;
    16. public Transform content;
    17.  
    18. List<GameObject> levelButtons;
    19.  
    20. public string simpleModeScene;
    21.  
    22. public static LevelManager instance;
    23.  
    24. private void Start()
    25. {
    26.    if (instance == null)
    27.    {
    28.        instance = this;
    29.    }
    30.    levelButtons = new List<GameObject>();
    31. }
    32.  
    33. public void SimpleModeButton()
    34. {
    35.    CommonInfos.selectedMode = Mode.SimpleMode;
    36.    CreateButtons(CommonInfos.selectedMode, numberOfSimpleLevels);
    37. }
    38. public void ClassicModeButton()
    39. {
    40.    CommonInfos.selectedMode = Mode.ClassicMode;
    41.    CreateButtons(CommonInfos.selectedMode, numberOfClassicLevels);
    42. }
    43.  
    44. public void CreateButtons(Mode mode, int totalLevels)
    45. {
    46.  
    47.    foreach (var item in levelButtons)
    48.    {
    49.        Destroy(item);
    50.    }
    51.    levelButtons.Clear();
    52.  
    53.    for (int i = 0; i < totalLevels; i++)
    54.    {
    55.        GameObject btn = Instantiate(levelButton, content);
    56.        btn.SetActive(true);
    57.        btn.name = i.ToString();
    58.        levelButtons.Add(btn);
    59.    }
    60.    SetButtons(mode);
    61. }
    62.  
    63.  
    64. public void SetButtons(Mode mode)
    65. {
    66.    Debug.Log(PlayerPrefs.GetInt(mode + CommonInfos.unlockedLevels, 0));
    67.    for (int i = 0; i < levelButtons.Count; i++)
    68.    {
    69.        if (i <= PlayerPrefs.GetInt(mode + CommonInfos.unlockedLevels, 0))
    70.        {
    71.            int index = i;
    72.            levelButtons[i].GetComponent<Button>().onClick.AddListener(() => SceneToLoad(index));
    73.            levelButtons[i].transform.Find("text").gameObject.GetComponent<TextMeshProUGUI>().text = (i + 1).ToString();
    74.            levelButtons[i].transform.Find("lockImg").gameObject.SetActive(false);
    75.            int stars = PlayerPrefs.GetInt(mode + index + "levelStars", 3);
    76.            switch (stars)
    77.            {
    78.                case 2:
    79.                    levelButtons[i].transform.Find("1").gameObject.SetActive(true);
    80.                    levelButtons[i].transform.Find("2").gameObject.SetActive(false);
    81.                    levelButtons[i].transform.Find("3").gameObject.SetActive(false);
    82.                    break;
    83.  
    84.                case 1:
    85.                    levelButtons[i].transform.Find("1").gameObject.SetActive(true);
    86.                    levelButtons[i].transform.Find("2").gameObject.SetActive(true);
    87.                    levelButtons[i].transform.Find("3").gameObject.SetActive(false);
    88.                    break;
    89.  
    90.                case 0:
    91.                    levelButtons[i].transform.Find("1").gameObject.SetActive(true);
    92.                    levelButtons[i].transform.Find("2").gameObject.SetActive(true);
    93.                    levelButtons[i].transform.Find("3").gameObject.SetActive(true);
    94.                    break;
    95.  
    96.                default:
    97.                    levelButtons[i].transform.Find("1").gameObject.SetActive(false);
    98.                    levelButtons[i].transform.Find("2").gameObject.SetActive(false);
    99.                    levelButtons[i].transform.Find("3").gameObject.SetActive(false);
    100.                    break;
    101.            }
    102.        }
    103.    }
    104. }
    105.  
    106. public void SceneToLoad(int index)
    107. {
    108.    CommonInfos.levelToLoad = index;
    109.    loadingPanel.SetActive(true);
    110. }
    111.  
    112. }
    I have also checked the PlayerPrefs value of that level it remains same and some how changes in SetButtons function
     
    Last edited: Jan 16, 2022
  2. YBtheS

    YBtheS

    Joined:
    Feb 22, 2016
    Posts:
    239
    This seems like a programming problem rather than a game design one. Try asking in the Scripting sub-forum instead.