• 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
Question by Andrzej828 · Oct 31, 2016 at 10:07 PM · inspectorinventorypickupitempick up

Picking up various types of items?

Hello, I am trying to work up a simple RPG system, where the player will be able to pick up items and add them to the inventory. The posts I have looked at on this forum haven't quite answered my question; I know this is quite a basic idea, but I would like some help with world game objects containing items that the player can pick up. The way I have it set up is that an object would have a script which would have a variable of a type that extends a class called BaseItem. The player's RayCast hits the item, and the player's equipped item is set to the world object's item, and destroys the object. The problem arises when I would have multiple items. Each item world object has its own "Tag", and its own separate script that applies an item to it. How can I make it so that one same script and tag can be used for each item?

Here is my code for one item, "Simple Belt":

 using UnityEngine;
 using System.Collections;
 //><>
 public class SimpleBag : MonoBehaviour {
     //This class defines the Monobehaviour script for a SimpleBag container GameObject
     //It will store some Bag (in this case, a 'Simple Bag') for the player to pick up and equip
 
     private BasePack bag;
 
     // Creates the bag that the GameObject will contain
     void Start () {
         bag = new BasePack("Simple Bag", 5, 2, 100, 25);
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 
     //Getter and Setter for BasePack
     public BasePack Bag{
         get{
             return this.bag;
         }
         set{
             this.bag = value;
         }
     }
 }
 

And here is the code for a Simple Belt world item:

 using UnityEngine;
 using System.Collections;
 //><>

 public class SimpleBelt : MonoBehaviour {
     //This class defines the Monobehaviour script for a SimpleBelt container GameObject
     //It will store some Belt (in this case, a 'Simple Belt') for the player to pick up and equip
 private BaseBelt belt;

 // Creates the bag that the GameObject will contain
 void Start () {
     belt = new BaseBelt("Simple Belt", 3, 2, 9);
     belt.addWeaponSlot(new BeltSlot((int)BaseBelt.weaponSlots.PRIMARY));
     belt.addWeaponSlot(new BeltSlot((int)BaseBelt.weaponSlots.PRIMARY));
     belt.addWeaponSlot(new BeltSlot((int)BaseBelt.weaponSlots.SIDEARM));
     belt.addWeaponSlot(new BeltSlot((int)BaseBelt.weaponSlots.SIDEARM));
     belt.addWeaponSlot(new BeltSlot((int)BaseBelt.weaponSlots.BOW));
 }
 // Update is called once per frame
 void Update () {

 }

 //Getter and Setter for BasePack
 public BaseBelt Belt{
     get{
         return this.belt;
     }
     set{
         this.belt = value;
     }
 }
 }

Sorry if this post is too convoluted. Essentially, is there a way for me to scrap these scripts and make one script that 1) Initializes the item in the container world object, which can differ across all the different item types (perhaps through the Inspector), and 2) have the player be able to pick any of them up as if they were the same item (then later use the "is" keyword to check which type of item it is after it is picked up.

Thank you in advance :)

Comment

People who like this

0 Show 0
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.

Update about the future of Unity Answers

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta later in June. Please note, we are aiming to set Unity Answers to read-only mode on the 31st of May in order to prepare for the final data migration.

For more information, please read our full announcement.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Pickup Script Problem 1 Answer

inventory and item system where every item need its own use function 0 Answers

How to add "Properties" to items? 1 Answer

Creating inventory system with pickup problem 1 Answer

How do I let the player pick up an item with left click? 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