Search Unity

Save Item that was brought from the shop and equip current item............

Discussion in 'Scripting' started by Tatsumi-Kun, Nov 23, 2016.

  1. Tatsumi-Kun

    Tatsumi-Kun

    Joined:
    Feb 11, 2015
    Posts:
    130
    I have a shop which i buy weapon and after i brought that specific weapon, i want to save it using playerprefs and equip current weapon that was brought and save the current equip weapon , each button have a script with the price and weapon number is being selected and save so that the player can use it when the game start or level.....

    Code (CSharp):
    1.  
    2. public class TestShop : MonoBehaviour {
    3.     public int Cannon;
    4.     public GameObject Equipimage;
    5.     public int Cost;
    6.     public int wasPurchase;
    7.     public GameObject BuyButton,EquipButton;
    8.     public bool equip;
    9.     void Awake ()
    10.     {
    11.         wasPurchase = PlayerPrefs.GetInt ("WasPurchase", 0);
    12.  
    13.         if (wasPurchase >= Cannon) {
    14.             BuyButton.SetActive (false);
    15.             EquipButton.SetActive (true);
    16.             equip = PlayerPrefs.GetInt ("Equiped") == 1 ? true : false;
    17.         }
    18.  
    19.  
    20.  
    21.  
    22.     }
    23.  
    24.     void Update ()
    25.     {
    26.  
    27.         if (equip == true) {
    28.             Equipimage.SetActive (true);
    29.         } else {
    30.             Equipimage.SetActive (false);
    31.         }
    32.     }
    33.  
    34.     public void Buy_Weapon ()
    35.     {
    36.         if (coins.cointCount >= Cost)
    37.         {
    38.             coins.goldRemaining = coins.cointCount - Cost;
    39.             coins.cointCount = coins.goldRemaining;
    40.             wasPurchase++;
    41.             PlayerPrefs.SetInt ("WasPurchase", wasPurchase);
    42.             BuyButton.SetActive (false);
    43.             EquipButton.SetActive (true);
    44.             //equip = true;
    45.             //PlayerPrefs.SetInt ("Equiped", ((equip) ? 1 : 0));
    46.         }
    47.     }
    48.  
    49.  
    50.     public void Equip_Weapon()
    51.     {
    52.         print ("Weapon Equip");
    53.         equip = true;
    54.         PlayerPrefs.SetInt ("Equiped", equip ? 1 : 0);
    55.         PlayerPrefs.Save ();
    56.     }
    57.  
    58.     public void ResetButton()
    59.     {
    60.        
     
  2. absolute_disgrace

    absolute_disgrace

    Joined:
    Aug 28, 2016
    Posts:
    253
    First off, i'd suggest using a serializable class and storing to a datafile rather than using Playerprefs.

    Secondly, i'm not exactly sure what you are attempting to ask in this post? The way you have described your solution sounds like you know what you need to do. You want to assign to your weapons a value and whenever a player purchases that weapon, you assign that number to his equip field.

    An further to this solution could be to create an array of booleans that cover each weapon. When a weapon is purchased, you could set the appropriate weapon slot to true. Then in your "equip" stat, you simply store which weapon is currently equipped (array index integer). If you need to display a list of weapons available, you can loop through the array and list each weapon that is marked as true.

    If you were using a serializable class, you could save that array and the 'equipped' int to the file.
     
  3. Tatsumi-Kun

    Tatsumi-Kun

    Joined:
    Feb 11, 2015
    Posts:
    130
    thanks i will try that!
     
  4. Tatsumi-Kun

    Tatsumi-Kun

    Joined:
    Feb 11, 2015
    Posts:
    130
    i have gotten a script on the internet and i create the array of boolean but it's in a array of class and when i purchase i a weapon, purchase is being set true. but saving it is a problem now after this i am going to look after equipping the weapon but i am getting these two error.

    Assets/TestShop.cs(33,50): error CS1502: The best overloaded method match for `UnityEngine.PlayerPrefs.SetInt(string, int)' has some invalid arguments


    Assets/TestShop.cs(33,74): error CS1503: Argument `#2' cannot convert `bool' expression to type `int'


    i don't know if the foreach loop causing the error if you know a alternative method please inform me!


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class TestShop : MonoBehaviour {
    7.  
    8.     public GameObject button;
    9.     [System.Serializable]
    10.     public class Shopitem
    11.     {
    12.         public string Name;
    13.         public int Prices;
    14.         public Sprite WeaponIcon;
    15.         public bool purchase;
    16.        
    17.     }
    18.  
    19.     public Shopitem[] shopitem;
    20.  
    21.     void Start ()
    22.     {
    23.         foreach (Shopitem I in shopitem) {
    24.        
    25.             GameObject btn = (GameObject)Instantiate (button);
    26.             ItemDetail ID = btn.GetComponent<ItemDetail> ();
    27.             ID.weaponName.text = I.Name;
    28.             ID._cost.text = "Cost:" + I.Prices.ToString ();
    29.             ID.Icon.sprite = I.WeaponIcon;
    30.             Shopitem thisItem = I;
    31.             ID.BuyButton.onClick.AddListener (Purchase);
    32.             ID.BuyButton.onClick.AddListener (PlayerPrefs.SetInt("waspurchase", I.purchase) ? 1: 0);
    33.  
    34.             btn.transform.SetParent (this.transform);
    35.         }
    36.     }
    37.  
    38.  
    39.  
    40.     void Purchase ()
    41.     {
    42.        
    43.  
    44.         print ("just purchase");
    45.  
    46.     }
     
  5. absolute_disgrace

    absolute_disgrace

    Joined:
    Aug 28, 2016
    Posts:
    253
    The error message you are getting should indicate what exact line of code it does not like. So you have 2 errors there each which will differ as to why they aren't working.

    Which exact lines of code are they referring to? Can you post the methods they are in?