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 :)
Your answer
Follow this Question
Related Questions
Pickup Script Problem 1 Answer
List check argument of item 3 Answers
Inventory Master 0 Answers
int variable from dictionary? 0 Answers
inventory and item system where every item need its own use function 0 Answers