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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Can't get in-game shop to work

Discussion in 'Scripting' started by j7stin, Feb 15, 2020.

  1. j7stin

    j7stin

    Joined:
    Jan 7, 2020
    Posts:
    22
    Hello everyone,

    This may be a bit of a long post but I really need help, I'm quite desperate and I have nowhere else to go so I beg, have a look.

    I'm trying to set up a shop in Unity so that my players can go buy items with the money they earn in game.
    I have seperate scenes for every level and a seperate scene for my shop.
    In the shop, the player can choose what material they want their player cube to be made out of.
    They have 3 options: Default (Free), Blue ($150) and Green ($150).

    I'm currently storing their choices as playerprefs as I want them to be accessible in different scenes.
    I need to make it so that once they've purchased something, they can tap on that item and it won't purchase again, it will just be equipped.

    This is essentially the code for the shop. It's the purchase code.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.  
    6.  
    7. public class Purchase : MonoBehaviour
    8. {
    9.  
    10.     public MoneyManager theMM;
    11.  
    12.     public bool defaultplayerbool;
    13.     public bool blueplayerbool;
    14.  
    15.     public bool blueplayerboolisowned;
    16.  
    17.     public bool greenplayerbool;
    18.     public bool greenplayerboolisowned;
    19.     public void BluePlayerPurchase()
    20.     {
    21.  
    22.         if (PlayerPrefs.GetInt("blueplayerboolisowned") == 1)
    23.         {
    24.             blueplayerboolisowned = true;
    25.         } else
    26.         {
    27.             blueplayerboolisowned = false;
    28.         }
    29.  
    30.         if (blueplayerboolisowned)
    31.         {
    32.             defaultplayerbool = false;
    33.             greenplayerbool = false;
    34.             blueplayerbool = !blueplayerbool;
    35.             PlayerPrefs.SetInt("blueplayerbool", blueplayerbool ? 1 : 0);
    36.         } else if (theMM.moneyAmount > 150 && blueplayerboolisowned == false)
    37.         {
    38.             defaultplayerbool = false;
    39.             greenplayerbool = false;
    40.             blueplayerboolisowned = true;
    41.             PlayerPrefs.SetInt("blueplayerboolisowned", blueplayerboolisowned ? 1 : 0);
    42.             theMM.moneyAmount -= 150;
    43.             PlayerPrefs.SetInt("MoneyAmount", theMM.moneyAmount);
    44.             blueplayerbool = !blueplayerbool;
    45.             PlayerPrefs.SetInt("blueplayerbool", blueplayerbool ? 1 : 0);
    46.         }
    47.        
    48.     }
    49.  
    50.     public void GreenPlayerPurchase()
    51.     {
    52.  
    53.         if (PlayerPrefs.GetInt("greenplayerboolisowned") == 1)
    54.         {
    55.             greenplayerboolisowned = true;
    56.         }
    57.         else
    58.         {
    59.             greenplayerboolisowned = false;
    60.         }
    61.  
    62.         if (greenplayerboolisowned)
    63.         {
    64.             defaultplayerbool = false;
    65.             blueplayerbool = false;
    66.             greenplayerbool = !greenplayerbool;
    67.             PlayerPrefs.SetInt("greenplayerbool", greenplayerbool ? 1 : 0);
    68.         }
    69.         else if (theMM.moneyAmount > 150 && greenplayerboolisowned == false)
    70.         {
    71.             defaultplayerbool = false;
    72.             blueplayerbool = false;
    73.             greenplayerboolisowned = true;
    74.             PlayerPrefs.SetInt("greenplayerboolisowned", greenplayerboolisowned ? 1 : 0);
    75.             theMM.moneyAmount -= 150;
    76.             PlayerPrefs.SetInt("MoneyAmount", theMM.moneyAmount);
    77.             greenplayerbool = !greenplayerbool;
    78.             PlayerPrefs.SetInt("greenplayerbool", greenplayerbool ? 1 : 0);
    79.         }
    80.  
    81.     }
    82.  
    83.     public void DefaultPlayerPurchase()
    84.     {
    85.         blueplayerbool = false;
    86.         greenplayerbool = false;
    87.         defaultplayerbool = !defaultplayerbool;
    88.         PlayerPrefs.SetInt("defaultplayerbool", defaultplayerbool ? 1 : 0);
    89.     }
    90. }
    91.  

    Then this is a section of code from the GameManager script and prefab which has been added to every scene:

    Code (CSharp):
    1.     public GameObject player;
    2.  
    3.     public Material blueplayermat;
    4.     public Material defaultplayermat;
    5.     public Material greenplayermat;
    6.  
    7.     private void Start()
    8.     {
    9.  
    10.         if (PlayerPrefs.GetInt("defaultplayerbool") == 1)
    11.         {
    12.             player.GetComponent<MeshRenderer>().material = defaultplayermat;
    13.         } else if (PlayerPrefs.GetInt("blueplayerbool") == 1)
    14.         {
    15.             player.GetComponent<MeshRenderer>().material = blueplayermat;
    16.         } else if (PlayerPrefs.GetInt("greenplayerbool") == 1)
    17.         {
    18.             player.GetComponent<MeshRenderer>().material = greenplayermat;
    19.         }
    20.     }

    That's just a snippet of the code but that's all the code that applies to the purchase script.

    I've done a little debugging and noticed that once all skins/player mats are purchased, the playerprefs "defaultplayerbool", "greenplayerbool" and "blueplayerbool" do not change and they always stay at a value of 1. The player will always have the default material applied to them.

    Is there a more efficient way of doing this that actually works?

    Thanks :))
     
  2. mustafatat

    mustafatat

    Joined:
    Oct 1, 2017
    Posts:
    6
    Hello,

    If the player buys both green bait blue, you must choose which one you want to use. If I was I would do it like this.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class purc : MonoBehaviour
    6. {
    7.  
    8.     void Start()
    9.     {
    10.         if(PlayerPrefs.GetString("SelectedMaterial")=="Blue")
    11.         {
    12.             player.GetComponent<MeshRenderer>().material = blueplayermat;
    13.         }
    14.         else if(PlayerPrefs.GetString("SelectedMaterial") == "Green")
    15.         {
    16.             player.GetComponent<MeshRenderer>().material = greenplayermat;
    17.         }
    18.         else
    19.         {
    20.             player.GetComponent<MeshRenderer>().material = defaultplayermat; ;
    21.         }
    22.     }
    23.  
    24.     public void BluePurchase()
    25.     {
    26.         if (theMM.moneyAmount > 150 && PlayerPrefs.GetInt("Blue") == 0)
    27.         {
    28.             PlayerPrefs.SetInt("Blue", 1);
    29.             theMM.moneyAmount -= 150;
    30.         }
    31.     }
    32.  
    33.     public void GreenPurchase()
    34.     {
    35.         if (theMM.moneyAmount > 150 && PlayerPrefs.GetInt("Green") == 0)
    36.         {
    37.             PlayerPrefs.SetInt("Green", 1);
    38.             theMM.moneyAmount -= 150;
    39.         }
    40.     }
    41.  
    42.     public void SelectBlueMaterialButtonOnClick()
    43.     {
    44.         if(PlayerPrefs.GetInt("Blue")==1)
    45.         {
    46.             PlayerPrefs.SetString("SelectedMaterial", "Blue");
    47.         }
    48.     }
    49.  
    50.     public void SelectGreenMaterialButtonOnClick()
    51.     {
    52.         if (PlayerPrefs.GetInt("Green") == 1)
    53.         {
    54.             PlayerPrefs.SetString("SelectedMaterial", "Green");
    55.         }
    56.     }
    57.  
    58. }
    59.  
     
  3. j7stin

    j7stin

    Joined:
    Jan 7, 2020
    Posts:
    22
    Hello, thanks for the reply :)

    Your code works but when I enter my level and I Debug.Log the SelectedMaterial playerprefs, it tells me the right mat and then the player mat doesn't update.

    The playermat only updates in level2, in the other levels the SelectedMaterial is set properly but the playermat does not update.
     
  4. mustafatat

    mustafatat

    Joined:
    Oct 1, 2017
    Posts:
    6
    Checking the adjustments you made in level2 and applying these settings to other level scenes may solve the problem. If it works at Level2, it should work at others