Item pick up system

I’m working on a game with strong RPG elements; item pick up is a good example of this. I want to create a system that involves placing a dummy prefab on the ground with a script (1. pick up helper script) containing, or leading into a database containing (whichever is quicker) properties of the item being picked up. This script(1) then translates these properties to the player’s inventory system which uses the data provided by script(1) to display this item in the player’s inventory, decide what assets to use if the player equips the item, and confer any stat bonuses afforded by the item if it is equipped. My questions are these: what types of messages or unity commands could I use to transfer information in that way? and would using a database of item properties be beneficial in terms of efficiency?

I can’t provide any code that would fit your particular needs, but here’s a basic outline how I would do it:

The dummy object has of course a collider attached. Set the object’s tag to “item” or whatever you create for it.

When clicked, check if the MainCamera.ScreenPointToRay (Input.mousePosition) ray hits the dummy’s collider, and check if the object you hit has an item tag.

If you are sure it was an item, just store the gameobject either directly into your inventory array/list (containg item GameObjects), OR, if you want it to “pick up” first into your virtual “hand” so you could drop it again or put it into your inventory from there, assign the clicked item to a “hand slot” GameObject variable. Then you could put it into your inventory array if you click on a GUI element representing an inventory slot, but be sure to set the hand slot to null (when dropping the item as well). Whenever the item is picked up from your world, set collider.enabled and renderer.enabled to false, and to true when you drop it again into the world.

If you want to have different item types, you could just add a simple int variable to your item script, like 0 = melee weapon, 1 = ranged weapon, 2 = healing item and so on. This could let the game/inventory identify which kind of item it is, and you only need one kind of tag.

Hope that helps get you started.

Here’s how my item script looks, it has enums so you can select which kind of item it is without having to use multiple item scripts (helps a lot when your game only need to check for one kind of item script, letting the “type” variable inside tell what it actually is)

//select a specific item from the drop down lists below in //the inspector. Note that only the last one selected gets //accepted if you have more than one, so be sure to set one //to your item and the rest to NONE.

var RangedWeapon : RangedWeaponEnum;
enum RangedWeaponEnum
{
	None,
	Colt1911
}

var MeleeWeapon : MeleeWeaponEnum;
enum MeleeWeaponEnum
{
	None,
	CombatKnife
}

var Type : TypeEnum;//don't touch this one, it gets assigned by the game at start!
enum TypeEnum
{
	RangedWeapon,
	MeleeWeapon,
	HealingItem
}

//item statistics

var Name : String;
var Description : String;
var Weight : float = 1.0; //weight in kilograms
var Condition : float = 100;
var Stackable : boolean;
var MaxStackSize : int;
var Damage : int;
//how much damage is dealt each hit (or how much is healed if it's a healing item)

var Owner : GameObject;//who is the current owner? change when picked up or dropped (null).


//check which kind of item was selected in the inspector, and assign the item type (melee, //ranged etc.) according to it. The type variable is mainly used for the UseItem function below.
function Start () 
{
     if(RangedWeapon != 0)
     {
	 Type = 0;
	 if(RangedWeapon == 1){Colt1911();}
	 if(RangedWeapon == 2){Tommygun();}
         if(RangedWeapon == 3){BoltActionRifle();}
     }
     if(MeleeWeapon != 0)
     {
         Type = 1;
 	 if(MeleeWeapon == 1){CombatKnife();}
	 if(MeleeWeapon == 2){MeatCleaver();}
	 if(MeleeWeapon == 3){CavalrySabre();}
     }
     if(Healing != 0)
     {
     Type = 2;
     if(Healing == 1){MedKit();}
     }
}

function HideItem()//when it's picked up
{
	transform.position = Vector3(0,0,0);
	renderer.enabled = false;
	collider.enabled = false;
}

function ShowItem()//when it's dropped, world position has to be set too
{
	renderer.enabled = true;
	collider.enabled = true;
}

//what happens when the item is used?
function UseItem(Target : GameObject, TargetPos : Vector3)
{

     if(Type == 0)//ranged weapon
     {
          //deal damage to target etc.
     }
     if(Type == 1)//melee weapon
     {
          //deal damage to target etc.
     }
     if(Type == 2)//healing item
     {
          //Heal user
     }
}

function Colt1911()
{
	Name = "Colt 1911";
	Weight = 1.0;
	Stackable = false;
	//MaxStackSize = 5;
	Description = "The venerable Colt 1911 in .45 calibre.";
	
	Damage = 10;
}

function CombatKnife()
{
	Name = "Combat Knife";
	Weight = 1.0;
	Stackable = false;
	//MaxStackSize = 5;
	Description = "A heavy Bowie knife.";
	
	Damage = 10;
}

function MedKit()
{
	Name = "MedKit";
	Weight = 1.0;
	Stackable = true;
	MaxStackSize = 2;
	Description = "A small pouch containing various medical supplies designed for quick use during and after combat.";
	
	Damage = 50;

}

//... and so on. basically, you have one function per specific item that is called at start to //assign all item values.