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 /
This question was closed Jul 05, 2018 at 05:32 PM by KelseySHock for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by KelseySHock · Jul 05, 2018 at 02:36 AM · inventoryinventory system

Making Inventory System Work for Multiple Food Types

I have a bomb inventory system implemented but right now I only have it working for 1 item (a piece of cake). How can I make this work for multiple types of food without copying and pasting my scripts over and over!

My script I put on the player:

public class InventoryItems : MonoBehaviour {

 Foods foods;
 public Inventory inventory;
 Text text;

 public int numOfCakes = 0;
 public int cakeW = 1;
 public int cakeS = 2;

 void Awake()
 {
     numOfCakes = ES2.Load<int>("numOfCakes");
 }

 // Use this for initialization
 void Start () {
     foods = FindObjectOfType<Foods>();
     text = FindObjectOfType<Text>();
 }
 
 // Update is called once per frame
 void Update () {
     
 }

 private void OnTriggerStay(Collider other)
 {
     if (Input.GetKey(KeyCode.E))
     {
         if (other.tag == "Food")
         {
             if (foods.currentHungerPoints >= 1400
                 && inventory.weight + cakeW < 30
                 && inventory.size + cakeS < 40)
             {
                 numOfCakes += 1;
                 inventory.weight += cakeW;
                 inventory.size += cakeS;
                 Destroy(other.gameObject);
             }
             else if (foods.currentHungerPoints + foods.cakeCal >= 1500
                     && inventory.weight + cakeW > 30
                     && inventory.size + cakeS < 40)
             {
                 text.text = "Too heavy to carry!";
                 return;
             }
             else if (foods.currentHungerPoints + foods.cakeCal >= 1500
                     && inventory.weight + cakeW < 30
                     && inventory.size + cakeS > 40)
             {
                 text.text = "Too bulky to carry!";
                 return;
             }
             else if (foods.currentHungerPoints + foods.cakeCal >= 1500
                     && inventory.weight + cakeW > 30
                     && inventory.size + cakeS > 40)
             {
                 text.text = "Too bulky and heavy to carry!";
                 return;
             }
         }
     }
 }

 void OnApplicationQuit()
 {
     ES2.Save(numOfCakes, "numOfCakes");
 }

}

And my other script that I put on my Inventory canvas

public class Inventory : MonoBehaviour {

 public Text text;
 public Button button;
 Foods foods;
 InventoryItems inventoryItems;

 public int weight;
 public int size;

 void Awake()
 {
     weight = ES2.Load<int>("weight");
     size = ES2.Load<int>("size");
 }

 // Use this for initialization
 void Start()
 {
     foods = FindObjectOfType<Foods>();
     inventoryItems = FindObjectOfType<InventoryItems>();

     Button btn = button.GetComponent<Button>();
     button.onClick.AddListener(OnClick);
 }

 // Update is called once per frame
 void Update()
 {
     text = FindObjectOfType<Text>();
     text.text = "Inventory: Can carry 30kg/40L\n\n"
     + "\nCakes: " + inventoryItems.numOfCakes
     + "\n\nCurrent Weight: " + weight + "\nCurrent Size: " + size;
     ES2.Save(weight, "weight");
     ES2.Save(size, "size");
 }
 public void OnClick()
 {
     if (inventoryItems.numOfCakes > 0)
     {
         inventoryItems.numOfCakes -= 1;
         weight -= inventoryItems.cakeW;
         size -= inventoryItems.cakeS;
     }
 }

}

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

1 Reply

  • Sort: 
avatar image
0

Answer by madks13 · Jul 05, 2018 at 01:20 PM

Can you post the Foods class? The obvious solution would be to have a base Food class, containing common food data, then inheriting that class by all the food types you need in your game. You should also have a list of the base food class, so you can know what kinds of food you have stored inside.

Comment
Add comment · Share
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

Follow this Question

Answers Answers and Comments

88 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 avatar image avatar image avatar image avatar image

Related Questions

Inventory Drop Function Problem 1 Answer

What is a good component to use for a GUI inventory display? 1 Answer

How to optimize this script and add items imediately without grids 0 Answers

Inventory AddItem help 1 Answer

Scriptableobject List and Instantiating objects from it 3 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