Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Panays · Dec 17, 2016 at 08:35 PM · prefabsinventory systemitemsstorageshop

how can i update the value of a variable that is in a prefab button without duplicating the prefab

Hi all, So my aim i was trying to make a shop for where a player can buy items for his car. The Shop contains 6 different types of items and variety of them. After the player click on the prefab button I want the Prefab to appear in the Player's storage. So far i managed to populate the lists and successfully made the prefabs to appear in the storage. By using the updateStorage() function made it duplicates all the prefabs that are already bought. That is normal i was checking if the code works but how can i fix that so the Items that are already in the storage doesn't duplicate but the amount (count) of those parts increase 10 times each time. E.g each time the player clicks on the prefab button he gets x10 uses of that part if he clicks again i want the prefab button to stay one but have x20 uses of part. Any ideas , thoughts or coding help would be muchappreciate.

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class ShopHandler : MonoBehaviour {
 
     [System.Serializable]
     public class Part
     {
         public string name;
         public Sprite image;
         public string description;
         public int cost;
         public float gain;
         public int count;
         
     }
 
     public GameObject[] buttonParts;
 
     public GameObject TyreContainer, FrontWingContainer, RearWingContainer, SuspensionContainer, EngineContainer, SidepodContainer;
     public GameObject TyreContainerStorage, FrontWingContainerStorage, RearWingContainerStorage, SuspensionContainerStorage, EngineContainerStorage, SidepodContainerStorage;
 
     public Part[] TyreTypes,FrontWingTypes,RearWingTypes,SuspensionTypes,SidepodsTypes,EngineTypes;
  
 
     public GameObject ShopItemPrefab;
     int counter;
 
     // Use this for initialization
     void Start() {
         pupulateShop(TyreTypes, TyreContainer);
         pupulateShop(FrontWingTypes, FrontWingContainer);
         pupulateShop(RearWingTypes, RearWingContainer);
         pupulateShop(EngineTypes, EngineContainer);
         pupulateShop(SuspensionTypes, SuspensionContainer);
         pupulateShop(SidepodsTypes, SidepodContainer);
         updateStorage(); 
         // so it can load any parts that are already in storage
     }
     // In order to move the parts to the storage 
     void updateStorage() {
         LoadStorage(TyreTypes, TyreContainerStorage);
         LoadStorage(FrontWingTypes, FrontWingContainerStorage);
         LoadStorage(RearWingTypes, RearWingContainerStorage);
         LoadStorage(EngineTypes, EngineContainerStorage);
         LoadStorage(SuspensionTypes, SuspensionContainerStorage);
         LoadStorage(SidepodsTypes, SidepodContainerStorage);
     }
 
     public void pupulateShop(Part[] PartTypes,GameObject container)
     {
         buttonParts = new GameObject[PartTypes.Length];
         Debug.Log("PartType loaded");
         counter = 0;
         foreach (Part i in PartTypes)
         {
 
             GameObject btn = Instantiate(ShopItemPrefab) as GameObject;
 
             buttonParts[counter] = btn;
 
             ItemScript scp = btn.GetComponent<ItemScript>();
 
             scp.name.text = i.name;
             scp.cost.text = i.cost.ToString();
             scp.count.text = i.count.ToString();
             scp.gain.text = i.gain.ToString();
             scp.icon.sprite = i.image;
             scp.description.text = i.description;
 
             Part thisPart = i;
 
             scp.btnPartBuy.onClick.AddListener(() => Purchase(thisPart));
 
             btn.transform.SetParent(container.transform, false);
             counter++;
 
         }
 
     }
 
     void LoadStorage(Part[] PartTypes, GameObject containerStorage)
     {
         buttonParts = new GameObject[PartTypes.Length];
         counter = 0;
         foreach (Part i in PartTypes)
         {
             if (i.count != 0)
             {
                 GameObject btn = Instantiate(ShopItemPrefab) as GameObject;
 
                 buttonParts[counter] = btn;
 
                 ItemScript scp = btn.GetComponent<ItemScript>();
 
                 scp.name.text = i.name;
                 scp.cost.text = i.cost.ToString();
                 scp.count.text = i.count.ToString();
                 scp.gain.text = i.gain.ToString();
                 scp.icon.sprite = i.image;
                 scp.description.text = i.description;
 
                 Part thisPart = i;
 
                 scp.btnPartBuy.onClick.AddListener(() => SelectItemToUse(thisPart));
 
                 btn.transform.SetParent(containerStorage.transform, false);
                 counter++;
 
             }
         }
     }
 
     void Purchase(Part bought) {
         bought.count = bought.count + 10;
         Debug.Log("Item bought");
         Debug.Log(bought.count);
         updateStorage();
     }
     // Update is called once per fram
 
     void SelectItemToUse(Part PartSelected)
     {
         Debug.Log("Item Selected");
     }
 }


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

84 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Change the dropping prefab for each in-game drop 1 Answer

Unity Inventory Database ID Error 1 Answer

How to add item to inventory with these scripts 0 Answers

Save gameobjects in a list before destoying 0 Answers

Modifiable shop content 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges