Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Unlock new character

Discussion in 'Scripting' started by hoshyargames, May 4, 2021.

  1. hoshyargames

    hoshyargames

    Joined:
    May 2, 2021
    Posts:
    24
    with the following code , each new character is unlocked, however there are some problems.

    here is code:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class BuyScript2 : MonoBehaviour
    4. {
    5.     private int myChar;
    6.     public int price;
    7.  
    8.     public GameObject buyCarrotsScreen;
    9.     public GameObject unlockRabbitsScreen;
    10.  
    11.     void Awake()
    12.     {
    13.         myChar = GetComponent<BuyScript>().myChar;
    14.     }
    15.  
    16.     void Update()
    17.     {
    18.         if (myChar == 1)
    19.         {
    20.             GlobalValues.character = 1;
    21.             PersistentData.Save();
    22.         }
    23.  
    24.         //If This is Character 2 button
    25.         if (myChar == 2)
    26.         {
    27.             //If you have unlocked character 2
    28.             if (GlobalValues.character2Unlocked)
    29.             {
    30.                 //Selects character 2
    31.                 GlobalValues.character = 2;
    32.                 PersistentData.Save();
    33.             }
    34.             //If you havent unlocked character two
    35.             else
    36.             {
    37.                 //If you have greater or equal money than the price
    38.                 if (GlobalValues.money >= price)
    39.                 {
    40.                     //Substracts the price from your money
    41.                     GlobalValues.money = GlobalValues.money - price;
    42.                     GlobalValues.character2Unlocked = true;
    43.                     PersistentData.Save();
    44.                 }
    45.                 // show buy carrots from cafe bazzar screen
    46.                 else
    47.                 {
    48.                     buyCarrotsScreen.SetActive(true);
    49.                     unlockRabbitsScreen.SetActive(false);
    50.                 }
    51.             }
    52.         }
    53.  
    54.         //If This is Character 2 button
    55.         if (myChar == 3)
    56.         {
    57.             //If you have unlocked character 2
    58.             if (GlobalValues.character3Unlocked)
    59.             {
    60.                 //Selects character 2
    61.                 GlobalValues.character = 3;
    62.                 PersistentData.Save();
    63.             }
    64.             //If you havent unlocked character two
    65.             else
    66.             {
    67.                 //If you have greater or equal money than the price
    68.                 if (GlobalValues.money >= price)
    69.                 {
    70.                     //Substracts the price from your money
    71.                     GlobalValues.money = GlobalValues.money - price;
    72.                 }
    73.                 // show buy carrots from cafe bazzar screen
    74.                 else
    75.                 {
    76.                     buyCarrotsScreen.SetActive(true);
    77.                     unlockRabbitsScreen.SetActive(false);
    78.                 }
    79.             }
    80.         }
    81.  
    82.         //If This is Character 2 button
    83.         if (myChar == 4)
    84.         {
    85.             //If you have unlocked character 2
    86.             if (GlobalValues.character4Unlocked)
    87.             {
    88.                 //Selects character 2
    89.                 GlobalValues.character = 4;
    90.                 PersistentData.Save();
    91.             }
    92.             //If you havent unlocked character two
    93.             else
    94.             {
    95.                 //If you have greater or equal money than the price
    96.                 if (GlobalValues.money >= price)
    97.                 {
    98.                     //Substracts the price from your money
    99.                     GlobalValues.money = GlobalValues.money - price;
    100.                 }
    101.                 // show buy carrots from cafe bazzar screen
    102.                 else
    103.                 {
    104.                     buyCarrotsScreen.SetActive(true);
    105.                     unlockRabbitsScreen.SetActive(false);
    106.                 }
    107.             }
    108.         }
    109.  
    110.         //If This is Character 2 button
    111.         if (myChar == 5)
    112.         {
    113.             //If you have unlocked character 2
    114.             if (GlobalValues.character5Unlocked)
    115.             {
    116.                 //Selects character 2
    117.                 GlobalValues.character = 5;
    118.                 PersistentData.Save();
    119.             }
    120.             //If you havent unlocked character two
    121.             else
    122.             {
    123.                 //If you have greater or equal money than the price
    124.                 if (GlobalValues.money >= price)
    125.                 {
    126.                     //Substracts the price from your money
    127.                     GlobalValues.money = GlobalValues.money - price;
    128.                 }
    129.                 // show buy carrots from cafe bazzar screen
    130.                 else
    131.                 {
    132.                     buyCarrotsScreen.SetActive(true);
    133.                     unlockRabbitsScreen.SetActive(false);
    134.                 }
    135.             }
    136.         }
    137.  
    138.         //If This is Character 2 button
    139.         if (myChar == 6)
    140.         {
    141.             //If you have unlocked character 2
    142.             if (GlobalValues.character6Unlocked)
    143.             {
    144.                 //Selects character 2
    145.                 GlobalValues.character = 6;
    146.                 PersistentData.Save();
    147.             }
    148.             //If you havent unlocked character two
    149.             else
    150.             {
    151.                 //If you have greater or equal money than the price
    152.                 if (GlobalValues.money >= price)
    153.                 {
    154.                     //Substracts the price from your money
    155.                     GlobalValues.money = GlobalValues.money - price;
    156.                 }
    157.                 // show buy carrots from cafe bazzar screen
    158.                 else
    159.                 {
    160.                     buyCarrotsScreen.SetActive(true);
    161.                     unlockRabbitsScreen.SetActive(false);
    162.                 }
    163.             }
    164.         }
    165.  
    166.         //If This is Character 2 button
    167.         if (myChar == 7)
    168.         {
    169.             //If you have unlocked character 2
    170.             if (GlobalValues.character7Unlocked)
    171.             {
    172.                 //Selects character 2
    173.                 GlobalValues.character = 7;
    174.                 PersistentData.Save();
    175.             }
    176.             //If you havent unlocked character two
    177.             else
    178.             {
    179.                 //If you have greater or equal money than the price
    180.                 if (GlobalValues.money >= price)
    181.                 {
    182.                     //Substracts the price from your money
    183.                     GlobalValues.money = GlobalValues.money - price;
    184.                 }
    185.                 // show buy carrots from cafe bazzar screen
    186.                 else
    187.                 {
    188.                     buyCarrotsScreen.SetActive(true);
    189.                     unlockRabbitsScreen.SetActive(false);
    190.                 }
    191.             }
    192.         }
    193.  
    194.         GetComponent<BuyScript2>().enabled = false;
    195.     }
    196. }
    197.  
    problems:

    even when money is subtracted after buying new character , by closing and reopening the game again, money amount does not change and come back to initial value.

    when buying a new character, its lock icon does not get shown.

    how i should fix these?

    please someone answer to this topic not other tips. thanks.
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    Honestly, your Update is doing way to much and is probably going to lead to all sorts of issues. You could really use a better system to reduce your code as well. It's not even about tips, this can help fix a lot of possible problems to keep you from struggling down the road.

    But as far as your money goes, I don't know what GlobalValues does or how you're saving out your money, but this code subtracts only. You need to save out the new value and then load that value the next time the game is opened.

    I do see you have a PersistantData.Save() call, but it counts on the characterUnlocked bool to be true, which I don't see you setting that value to true.

    Honestly, your code confuses me since you're trying to do this all in Update, which is running every frame and could potentially hit multiple times, so I have no clue if it's even going to work how you want.
     
    hoshyargames likes this.
  3. hoshyargames

    hoshyargames

    Joined:
    May 2, 2021
    Posts:
    24
    i like to get help from you. here are what i need also tell you to be more clear and thanks you really.
    please.

    Code (CSharp):
    1. public static class GlobalValues
    2. {
    3.     public static int money = 6500;
    4.  
    5.     public static int character;
    6.  
    7.     public static bool character2Unlocked;
    8.     public static bool character3Unlocked;
    9.     public static bool character4Unlocked;
    10.     public static bool character5Unlocked;
    11.     public static bool character6Unlocked;
    12.     public static bool character7Unlocked;
    13. }
    save and load:

    Code (CSharp):
    1. public class PersistentData : MonoBehaviour
    2. {
    3.     void Start()
    4.     {
    5.         Load();
    6.     }
    7.  
    8.     public static void Save()
    9.     {
    10.         BinaryFormatter binaryFormatter = new BinaryFormatter();
    11.         FileStream fileStream = File.Create(Application.persistentDataPath + "/playerData.dat");
    12.         PlayerData pD = new PlayerData();
    13.  
    14.         pD.money = GlobalValues.money;
    15.  
    16.         pD.character = GlobalValues.character;
    17.  
    18.         pD.character2unlocked = GlobalValues.character2Unlocked;
    19.         pD.character3unlocked = GlobalValues.character3Unlocked;
    20.         pD.character4unlocked = GlobalValues.character4Unlocked;
    21.         pD.character5unlocked = GlobalValues.character5Unlocked;
    22.         pD.character6unlocked = GlobalValues.character6Unlocked;
    23.         pD.character7unlocked = GlobalValues.character7Unlocked;
    24.  
    25.         binaryFormatter.Serialize(fileStream, pD);
    26.         fileStream.Close();
    27.     }
    28.  
    29.     public static void Load()
    30.     {
    31.         if (File.Exists(Application.persistentDataPath + "/playerData.dat"))
    32.         {
    33.             BinaryFormatter binaryFormatter = new BinaryFormatter();
    34.             FileStream fileStream = File.Open(Application.persistentDataPath + "/playerData.dat", FileMode.Open);
    35.             PlayerData pD = (PlayerData)binaryFormatter.Deserialize(fileStream);
    36.  
    37.             fileStream.Close();
    38.  
    39.             GlobalValues.money = pD.money;
    40.  
    41.             GlobalValues.character = pD.character;
    42.  
    43.             GlobalValues.character2Unlocked = pD.character2unlocked;
    44.             GlobalValues.character3Unlocked = pD.character3unlocked;
    45.             GlobalValues.character4Unlocked = pD.character4unlocked;
    46.             GlobalValues.character5Unlocked = pD.character5unlocked;
    47.             GlobalValues.character6Unlocked = pD.character6unlocked;
    48.             GlobalValues.character7Unlocked = pD.character7unlocked;
    49.         }
    50.         //else
    51.         //{
    52.         //    GlobalValues.character = GlobalValues.character;
    53.         //}
    54.     }
    55. }
    56.  
    57. [Serializable]
    58. class PlayerData
    59. {
    60.     public int money;
    61.     public int character;
    62.  
    63.     public bool character2unlocked;
    64.     public bool character3unlocked;
    65.     public bool character4unlocked;
    66.     public bool character5unlocked;
    67.     public bool character6unlocked;
    68.     public bool character7unlocked;
    69. }
    buy button scripts:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3.  
    4. public class BuyScript : MonoBehaviour
    5. {
    6.     public int myChar;
    7.     public Image lockIcon;
    8.     public Image unlockIcon;
    9.  
    10.  
    11.  
    12.     void Update()
    13.     {
    14.         if (myChar == 1)
    15.         {
    16.             if (GlobalValues.character == 1)
    17.             {
    18.                 unlockIcon.enabled = true;
    19.             }
    20.             else
    21.             {
    22.                 unlockIcon.enabled = false;
    23.                 lockIcon.enabled = false;
    24.             }
    25.         }
    26.  
    27.         if (myChar == 2)
    28.         {
    29.             //If it has been unlocked.
    30.             if (GlobalValues.character2Unlocked)
    31.             {
    32.                 // if it is selected.
    33.                 if (GlobalValues.character == 2)
    34.                 {
    35.                     unlockIcon.enabled = true;
    36.                 }
    37.                 // if it is not selected but unlocked.
    38.                 else
    39.                 {
    40.                     unlockIcon.enabled = false;
    41.                     lockIcon.enabled = false;
    42.                 }
    43.             }
    44.             //If it has not been unlocked.
    45.             else
    46.             {
    47.                 lockIcon.enabled = true;
    48.             }
    49.         }
    50.  
    51.         if (myChar == 3)
    52.         {
    53.             //If it has been unlocked.
    54.             if (GlobalValues.character3Unlocked)
    55.             {
    56.                 // if it is selected.
    57.                 if (GlobalValues.character == 3)
    58.                 {
    59.                     unlockIcon.enabled = true;
    60.                 }
    61.                 // if it is not selected but unlocked.
    62.                 else
    63.                 {
    64.                     unlockIcon.enabled = false;
    65.                     lockIcon.enabled = false;
    66.                 }
    67.             }
    68.             //If it has not been unlocked.
    69.             else
    70.             {
    71.                 lockIcon.enabled = true;
    72.             }
    73.         }
    74.  
    75.         if (myChar == 4)
    76.         {
    77.             //If it has been unlocked.
    78.             if (GlobalValues.character4Unlocked)
    79.             {
    80.                 // if it is selected.
    81.                 if (GlobalValues.character == 4)
    82.                 {
    83.                     unlockIcon.enabled = true;
    84.                 }
    85.                 // if it is not selected but unlocked.
    86.                 else
    87.                 {
    88.                     unlockIcon.enabled = false;
    89.                     lockIcon.enabled = false;
    90.                 }
    91.             }
    92.             //If it has not been unlocked.
    93.             else
    94.             {
    95.                 lockIcon.enabled = true;
    96.             }
    97.         }
    98.  
    99.         if (myChar == 5)
    100.         {
    101.             //If it has been unlocked.
    102.             if (GlobalValues.character5Unlocked)
    103.             {
    104.                 // if it is selected.
    105.                 if (GlobalValues.character == 5)
    106.                 {
    107.                     unlockIcon.enabled = true;
    108.                 }
    109.                 // if it is not selected but unlocked.
    110.                 else
    111.                 {
    112.                     unlockIcon.enabled = false;
    113.                     lockIcon.enabled = false;
    114.                 }
    115.             }
    116.             //If it has not been unlocked.
    117.             else
    118.             {
    119.                 lockIcon.enabled = true;
    120.             }
    121.         }
    122.  
    123.         if (myChar == 6)
    124.         {
    125.             //If it has been unlocked.
    126.             if (GlobalValues.character6Unlocked)
    127.             {
    128.                 // if it is selected.
    129.                 if (GlobalValues.character == 6)
    130.                 {
    131.                     unlockIcon.enabled = true;
    132.                 }
    133.                 // if it is not selected but unlocked.
    134.                 else
    135.                 {
    136.                     unlockIcon.enabled = false;
    137.                     lockIcon.enabled = false;
    138.                 }
    139.             }
    140.             //If it has not been unlocked.
    141.             else
    142.             {
    143.                 lockIcon.enabled = true;
    144.             }
    145.         }
    146.  
    147.         if (myChar == 7)
    148.         {
    149.             //If it has been unlocked.
    150.             if (GlobalValues.character7Unlocked)
    151.             {
    152.                 // if it is selected.
    153.                 if (GlobalValues.character == 7)
    154.                 {
    155.                     unlockIcon.enabled = true;
    156.                 }
    157.                 // if it is not selected but unlocked.
    158.                 else
    159.                 {
    160.                     unlockIcon.enabled = false;
    161.                     lockIcon.enabled = false;
    162.                 }
    163.             }
    164.             //If it has not been unlocked.
    165.             else
    166.             {
    167.                 lockIcon.enabled = true;
    168.             }
    169.         }
    170.     }
    171. }
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class BuyScript2 : MonoBehaviour
    4. {
    5.     private int myChar;
    6.     public int price;
    7.  
    8.     public GameObject buyCarrotsScreen;
    9.     public GameObject unlockRabbitsScreen;
    10.  
    11.     void Awake()
    12.     {
    13.         myChar = GetComponent<BuyScript>().myChar;
    14.     }
    15.  
    16.     void Update()
    17.     {
    18.         if (myChar == 1)
    19.         {
    20.             GlobalValues.character = 1;
    21.             PersistentData.Save();
    22.         }
    23.  
    24.         //If This is Character 2 button
    25.         if (myChar == 2)
    26.         {
    27.             //If you have unlocked character 2
    28.             if (GlobalValues.character2Unlocked)
    29.             {
    30.                 //Selects character 2
    31.                 GlobalValues.character = 2;
    32.                 PersistentData.Save();
    33.             }
    34.             //If you havent unlocked character two
    35.             else
    36.             {
    37.                 //If you have greater or equal money than the price
    38.                 if (GlobalValues.money >= price)
    39.                 {
    40.                     //Substracts the price from your money
    41.                     GlobalValues.money = GlobalValues.money - price;
    42.                     GlobalValues.character2Unlocked = true;
    43.                     PersistentData.Save();
    44.                 }
    45.                 // show buy carrots from cafe bazzar screen
    46.                 else
    47.                 {
    48.                     buyCarrotsScreen.SetActive(true);
    49.                     unlockRabbitsScreen.SetActive(false);
    50.                 }
    51.             }
    52.         }
    53.  
    54.         //If This is Character 2 button
    55.         if (myChar == 3)
    56.         {
    57.             //If you have unlocked character 2
    58.             if (GlobalValues.character3Unlocked)
    59.             {
    60.                 //Selects character 2
    61.                 GlobalValues.character = 3;
    62.                 PersistentData.Save();
    63.             }
    64.             //If you havent unlocked character two
    65.             else
    66.             {
    67.                 //If you have greater or equal money than the price
    68.                 if (GlobalValues.money >= price)
    69.                 {
    70.                     //Substracts the price from your money
    71.                     GlobalValues.money = GlobalValues.money - price;
    72.                 }
    73.                 // show buy carrots from cafe bazzar screen
    74.                 else
    75.                 {
    76.                     buyCarrotsScreen.SetActive(true);
    77.                     unlockRabbitsScreen.SetActive(false);
    78.                 }
    79.             }
    80.         }
    81.  
    82.         //If This is Character 2 button
    83.         if (myChar == 4)
    84.         {
    85.             //If you have unlocked character 2
    86.             if (GlobalValues.character4Unlocked)
    87.             {
    88.                 //Selects character 2
    89.                 GlobalValues.character = 4;
    90.                 PersistentData.Save();
    91.             }
    92.             //If you havent unlocked character two
    93.             else
    94.             {
    95.                 //If you have greater or equal money than the price
    96.                 if (GlobalValues.money >= price)
    97.                 {
    98.                     //Substracts the price from your money
    99.                     GlobalValues.money = GlobalValues.money - price;
    100.                 }
    101.                 // show buy carrots from cafe bazzar screen
    102.                 else
    103.                 {
    104.                     buyCarrotsScreen.SetActive(true);
    105.                     unlockRabbitsScreen.SetActive(false);
    106.                 }
    107.             }
    108.         }
    109.  
    110.         //If This is Character 2 button
    111.         if (myChar == 5)
    112.         {
    113.             //If you have unlocked character 2
    114.             if (GlobalValues.character5Unlocked)
    115.             {
    116.                 //Selects character 2
    117.                 GlobalValues.character = 5;
    118.                 PersistentData.Save();
    119.             }
    120.             //If you havent unlocked character two
    121.             else
    122.             {
    123.                 //If you have greater or equal money than the price
    124.                 if (GlobalValues.money >= price)
    125.                 {
    126.                     //Substracts the price from your money
    127.                     GlobalValues.money = GlobalValues.money - price;
    128.                 }
    129.                 // show buy carrots from cafe bazzar screen
    130.                 else
    131.                 {
    132.                     buyCarrotsScreen.SetActive(true);
    133.                     unlockRabbitsScreen.SetActive(false);
    134.                 }
    135.             }
    136.         }
    137.  
    138.         //If This is Character 2 button
    139.         if (myChar == 6)
    140.         {
    141.             //If you have unlocked character 2
    142.             if (GlobalValues.character6Unlocked)
    143.             {
    144.                 //Selects character 2
    145.                 GlobalValues.character = 6;
    146.                 PersistentData.Save();
    147.             }
    148.             //If you havent unlocked character two
    149.             else
    150.             {
    151.                 //If you have greater or equal money than the price
    152.                 if (GlobalValues.money >= price)
    153.                 {
    154.                     //Substracts the price from your money
    155.                     GlobalValues.money = GlobalValues.money - price;
    156.                 }
    157.                 // show buy carrots from cafe bazzar screen
    158.                 else
    159.                 {
    160.                     buyCarrotsScreen.SetActive(true);
    161.                     unlockRabbitsScreen.SetActive(false);
    162.                 }
    163.             }
    164.         }
    165.  
    166.         //If This is Character 2 button
    167.         if (myChar == 7)
    168.         {
    169.             //If you have unlocked character 2
    170.             if (GlobalValues.character7Unlocked)
    171.             {
    172.                 //Selects character 2
    173.                 GlobalValues.character = 7;
    174.                 PersistentData.Save();
    175.             }
    176.             //If you havent unlocked character two
    177.             else
    178.             {
    179.                 //If you have greater or equal money than the price
    180.                 if (GlobalValues.money >= price)
    181.                 {
    182.                     //Substracts the price from your money
    183.                     GlobalValues.money = GlobalValues.money - price;
    184.                 }
    185.                 // show buy carrots from cafe bazzar screen
    186.                 else
    187.                 {
    188.                     buyCarrotsScreen.SetActive(true);
    189.                     unlockRabbitsScreen.SetActive(false);
    190.                 }
    191.             }
    192.         }
    193.  
    194.         GetComponent<BuyScript2>().enabled = false;
    195.     }
    196. }
    197.  
    i use these 4 classes in order to save and load . and also to buy and unlock new character.
    now i hope to becomme fix things. thanks again.
     
  4. hoshyargames

    hoshyargames

    Joined:
    May 2, 2021
    Posts:
    24

    may you please give ideas how to avoid using Update method?
     
  5. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    It's hard to say how your setup in your scene is done. But normally if I'm setting up an unlock system, I have a system to check what character is selected. Then the user can click on a buy button and unlock the char. Then I would save everything.

    So, let's say there is a lineup of chars to unlock. A user selects the first one. I can grab info to display about the char including price, if they already own it, etc at that point and display what I need to, updating gui. If they don't own it and have enough money to buy it, then the buy button is active. Once purchased, I adjust money and save out that value as well as saving out the character owned.
     
    hoshyargames likes this.
  6. hoshyargames

    hoshyargames

    Joined:
    May 2, 2021
    Posts:
    24
    me also have the same system, however some small fixs needs to be found. i need to debug more. but if someone who already had this can help , i am pleased to hear. thanks.
     
  7. hoshyargames

    hoshyargames

    Joined:
    May 2, 2021
    Posts:
    24
    if i initialize Money from code below in the editor ui text does not show the value , but from other script changing that will work . why is that not working from code below?

    Code (CSharp):
    1. public static class GlobalValues
    2. {
    3.     public static int money;
    4.  
    5.     public static int character;
     
  8. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    These are just static variables. Changing them has nothing to do with updating UI.

    And if you mean in an inspector, since they are static variables, they don't show up in the inspector.
     
    Kurt-Dekker likes this.