Simple Inventory, top down shooter

I have some troubles with creating an inventory for my top down shooter game. The reason I post my question here is because all the other questions are way too advanced and above all too wide-ranged. I want to use a very simple pick-up and check system for my game.

It is very simple. The player walks in a world, where there are a few collectibles which he/she can pick up. He does that completely without a mouse, he just walks into it.

My problem isn’t actually the collision, I have that completely running, but its the inventory itself. I want to view my inventory by clicking a tab for example. You get a screen with all the picked up items and you can select and view them. Here you get a text which explains the item…

Can somebody help me with this?

The youtube link I looked up for you is very clear and informative. Or, if you don’t like coding, try the asset store: I’m sure you’ll find something that’ll suit your needs. But no one is going to write code for you, trust me. Try, and post your code when you get stuck, you’ll find people around are very helpful.

Ok, Here is my code…

function OnControllerColliderHit( Hit : ControllerColliderHit)

if( Hit.gameObject.name == "Cube") //Pick up a Cube
{
	Hit.gameObject.renderer.enabled = false;
}

if(Hit.gameObject.name == "Sphere") //Pick up a sphere
{
	Hit.gameObject.renderer.enabled = false;
}

if( Hit.gameObject.name == "Cylinder") //Pick up a Cylinder
{
	Hit.gameObject.renderer.enabled = false;
}

if(Hit.gameObject.name == "Capsule") //Pick up a Capsule
{
	Hit.gameObject.renderer.enabled = false;
}

This is for the collision so that the objects wil disappear in the world.
Now Im stuck. My idea is to create an empty game object and link an script file on it. In that script file I want to pause the game, bring up some kind of menu/layer where the items: Weapons, log files and maybe more are shown. There you can select objects to read the description…

Step by step:

  1. define a class for your inventory objects

  2. create a generic List called inventory :

    import System.Collections.Generic;
    inventory = new List.();
    3)Tag your objects as ojectsYouCanCarry, for ex.

  3. when the player collides with an object, destroy the game object and add the corresponding instance of YourBrandNewClassBlabla to your inventory List.

I suspect you should do some reading on classes first, though. They’re not that hard, you’ll see! Let me know how you’re progressing,.

Yes I Know that you’re not going to write some code, but I just wanted an simple solution, given by gregzo many thanks man!

Ok, I’m a little bit further.

I made an script where my Inventory ArrayList is made. I made an ArrayList so the size is not static.
I have 4 objects, a cube, cylinder, sphere and a capsule.

var inventoryArray = new ArrayList();

Then I looked again at my collision script, I made it good, it actually destroys my object.

function OnControllerColliderHit( Hit : ControllerColliderHit)
{
	if( Hit.gameObject.name == "Cube") //Pick up a Cube
	{
		Destroy(GameObject.Find("Cube"));//Destory the Cube and put in the inventory
		Debug.Log("Cube is destroyed");
		InventArray.add("kubus");
	}
	
	if(Hit.gameObject.name == "Sphere") //Pick up a sphere
	{
		Destroy(GameObject.Find("Sphere"));//Destory the Sphere and put in the inventory
		Debug.Log("Sphere is destroyed");
	}
	
	if( Hit.gameObject.name == "Cylinder") //Pick up a Cylinder
	{
		Destroy(GameObject.Find("Cylinder"));//Destory the Cylinder and put in the inventory
		Debug.Log("Cylinder is destroyed");
	}
	
	if(Hit.gameObject.name == "Capsule") //Pick up a Capsule
	{
		Destroy(GameObject.Find("Capsule"));//Destory the Capsule and put in the inventory
		Debug.Log("Capsule is destroyed");

Now I need to actually move certain objects to my Array.
I have a .txt file for the description of my object. And a .jpg for the picture in the inventory.
It is all 2D. I think I can do it with an OnGUI function delivered with Unity.

I also have a inventory, made with an text area. This isn’t good, but it will do for now.

var inventoryTrue:boolean;
var keyDelay:float = 1.0;

function Update()
{
	if(Input.GetKey(KeyCode.I))
	{
		Debug.Log("Pressed I and called the inventory");
		inventoryTrue = !inventoryTrue;
	}
	
}

function OnGUI()
{
	if(inventoryTrue)
	{
		GUI.TextArea(Rect(100,100,400,400), "Inventory: Kubus, a lot of other shit");
	}
}

I am a lot further. I now made an Inventory Script, and a LootableObject script.
But I’m stuck.
I cannot get an lootableobject in the Inventory…
Can someone help me out here?

static var statInventory : Inventory1;										//	To set an instance of this script
enum SlotType 	{Items, Weapons, Logs, Empty}

//	HELPER CLASSES
@System.Serializable														//	Our Representation of an InventoryItem
class InventoryItem 
{
	var itemName : String;													//	What the item will be called in the inventory
	var itemIcon : Texture;													//	What the item will look like in the inventory
	var itemDescription : String;											//	The description of the item
	var slotType : SlotType;												//	What slot the item will fit in
}


private var inventory : InventoryItem[];									//	Our master inventory (private)							
private var contentArray : InventoryItem[];									//	The array to contain the item being passed to and from the LootableObject

var inventoryWidth : int;													//	the number of columns to display the inventory in
var inventoryLength : int;													//	the size of the inventory in total number of slots

var iconWidthHeight : int;													//	The pixel size (height and width) of an inventory slot
var spacing : int;															//	Space between slots (in x and y)
var offSet : Vector2;														//	The start position of the inventory

var emptySlot : Texture;													//	This will be drawn when a slot is empty

private var openInventoryWindow : boolean;										//	Controls OnGUI and opens/closes the inventory window
								
private var inventoryWindow : Rect;											//	The dimensions of the inventory window

private var currentLootableItem : LootableObject;							//	The pointer to the current lootable item being processed
private var newLootableItem : LootableObject;								//	The pointer to a new lootable item to be processed

function Awake () 
{																			//	Setup the initial states of the variables
	statInventory = this;
	
	inventoryWindow = new Rect (10, 10, Screen.width - 10, Screen.height - 10);

	openInventoryWindow = false;

	currentLootableItem = null;
	
	inventory = new Array (inventoryLength);								//   Create & init the array to hold the inventory 
	for (var i : int = 0; i < inventory.length; i++) 
	{ 
		inventory *= null;* 
  • }*
    }

function Update ()
*{ *

  • if (Input.GetKeyUp (KeyCode.I))*

  • { // If the “i” key is pressed…*

  •  Debug.Log("I gedrukt!");*
    
  •  openInventoryWindow = !openInventoryWindow;										//	... toggle the inventory window.*
    
  • }*
    }

function OnGUI ()
{

  • // Inventory Window*
  • if (openInventoryWindow)*
  • { // If the “open inventory window” toggle is true*
  •  GUI.Window (1, inventoryWindow, DrawInventoryWindow, "Inventory");	//   The title of this window could be passed as a String from the LootableItem*
    
  • }*
    }

function DrawInventoryWindow ()
{ // The window function to draw the inventory window

  • if (GUI.Button (Rect (5,5,10,10), “”))*

  • {*

  •  CloseInventoryWindow ();*
    
  • }*

  • var j : int;*

  • var k : int;*

  • var currentInventoryItem : InventoryItem; // Establish a variable to hold our data*

  • var currentRect : Rect;*

  • for (var i : int = 0; i < inventory.length; i ++) { // Go through each row …*

  •  j = i / inventoryWidth;												//   ... divide by array by width to get rows...*
    
  •  k = i % inventoryWidth;												//   ... find the remainder by width to get columns...*
    

_ currentInventoryItem = inventory*; // … set this point in the matrix as our current point …_
_ currentRect = (new Rect (offSet.x + k * (iconWidthHeight + spacing), offSet.y + j * (iconWidthHeight + spacing), iconWidthHeight, iconWidthHeight));_
_
if (currentInventoryItem == null) // … if there is no item in the j-th row and the k-th column, draw a blank texture*_
* { *
* GUI.DrawTexture (currentRect, emptySlot);*
* }*
* else*
* {*
* GUI.DrawTexture (currentRect, currentInventoryItem.itemIcon);*
* }*

* // If there is an item at this location and there is a button click…*
* if (currentInventoryItem != null && GUI.Button (currentRect, “”, GUIStyle (“label”)))*
* {*
* if (Input.GetMouseButtonUp (0)) // … if that click is mouse button 0: see the description*
* { *
* showDescriptionItem(currentInventoryItem); // Get the description out*

* }*
* }*
* }*
}

function CloseInventoryWindow ()
{
* openInventoryWindow = false;*
}

function AddItem (item : InventoryItem)
{
* for (var i : int = 0; i < inventory.length; i ++) // Go through each row*
* { *
_ if (inventory == null) // If the position is empty…
* {
inventory = item; // … add the new item…
return (true); // … and exit the function.
}
}
Debug.Log (“Inventory is full”);
return (false);
}*_

function showDescriptionItem (item : InventoryItem)
{
* GUIText(“” + item.descriptionItem );*
* Debug.Log (“Cannot Show Description of this Item”);*
* return (false);*
}
Here is my LootableObject Script:
#pragma strict

private var contentArray : InventoryItem[]; // The current contents of this lootable object
private var thisLootableItem : LootableObject; // A reference to this instance of this script
private var clicked : boolean; // Trap for creating the current contents only once
private var canLoot : boolean; // Boolean to control TestDistance.
private var playerTransform : Transform; // A reference to the Player Transform
private var thisTransform : Transform;

function Start ()
{ // Setup the initial states of the variables

* thisTransform = this.transform;*
* playerTransform = GameObject.FindWithTag(“Player”).transform;*
* thisLootableItem = this;*
* clicked = false;*

}

function OnMouseDown()
*{ *
* Loot (); *
*} *

function Loot ()
{
* if (!clicked)*
* { // If not yet clicked, check Loot Table*
* clicked = true;*

* }*

* Destroy(this.gameObject);*

* //lootWindowOpen = true;*
* //TestDistance ();*
}

function UpdateContentArray (newContentArray : InventoryItem[]) // A function to receive an updated array from Inventory1.js
*{ *
* contentArray = newContentArray;*
}

I feel like I’m almost finished and have a working inventory.
Have a problem with the loot able object, I think I’m declaring things not correctly

//	LootableObject.js
//	Make the GameObject "Lootable".
//	This script will 
//	Attach this script to any GameObject that is "Lootable"

#pragma strict

private var theInventoryItem : InventoryItem;										//	The current contents of this lootable object
private var thisLootableItem : LootObject;										//	A reference to this instance of this script
private var clicked : boolean;													//	Trap for creating the current contents only once
private var canLoot : boolean;													//	Boolean to control TestDistance.
private var playerTransform : Transform;										//	A reference to the Player Transform
private var thisTransform : Transform;

var itemName : String;
var itemIcon: Texture2D;
var itemDescription : String;


private var Inventory : Inventory1;


function Start () 
{																			//	Setup the initial states of the variables
	//itemName = the object name
	itemName = this.gameObject.name;
	//itemIcon will get selected in Unity
	//itemDescription as well
	
	var theInventoryItem : InventoryItem;
	//Init thhe inventory item
	theInventoryItem.itemName = itemName;
	theInventoryItem.itemIcon = itemIcon;
	theInventoryItem.itemDescription = itemDescription;
	
	thisTransform = this.transform;
	playerTransform = GameObject.FindWithTag("Player").transform; 
	thisLootableItem = this;
	clicked = false;
	
}


function OnMouseDown() 
{																				
	Loot ();																	
}																				

function Loot ()
{
	if (!clicked) 
	{																//	If not yet clicked, check Loot Table
		clicked = true;
	}
	
	//Add the item to the inventory
	Inventory.AddItem(theInventoryItem);
	theInventoryItem = null;					//Set it to null
	
			
	
	
	//Destroy the game object
	Destroy(this.gameObject);
}

My inventory. I think this is almost finished and done, the loot able object is going wrong i think.

//	Inventory.js
//	This script manages the inventory and displays the inventory items in a grid pattern
//	Attach to your Inventory Manager Object
//	Based on the code posted by Der Dude on the Unity3D forums: http://forum.unity3d.com/viewtopic.php?t=11865

static var statInventory : Inventory1;										//	To set an instance of this script
enum SlotType 	{Items, Weapons, Logs, Empty}

//	HELPER CLASSES
@System.Serializable														//	Our Representation of an InventoryItem
class InventoryItem 
{
	var itemName : String;													//	What the item will be called in the inventory
	var itemIcon : Texture;													//	What the item will look like in the inventory
	var itemDescription : String;											//	The description of the item
	var slotType : SlotType;												//	What slot the item will fit in
}


private var inventory : InventoryItem[];									//	Our master inventory (private)							
private var contentArray : InventoryItem[];									//	The array to contain the item being passed to and from the LootObject

var inventoryWidth : int;													//	the number of columns to display the inventory in
var inventoryLength : int;													//	the size of the inventory in total number of slots

var iconWidthHeight : int;													//	The pixel size (height and width) of an inventory slot
var spacing : int;															//	Space between slots (in x and y)
var offSet : Vector2;														//	The start position of the inventory

var emptySlot : Texture;													//	This will be drawn when a slot is empty

private var openInventoryWindow : boolean;										//	Controls OnGUI and opens/closes the inventory window
								
private var inventoryWindow : Rect;											//	The dimensions of the inventory window

private var currentLootableItem : LootObject;							//	The pointer to the current lootable item being processed
private var newLootableItem : LootObject;								//	The pointer to a new lootable item to be processed

function Awake () 
{																			//	Setup the initial states of the variables
	statInventory = this;
	
	inventoryWindow = new Rect (10, 10, Screen.width - 10, Screen.height - 10);

	openInventoryWindow = false;

	currentLootableItem = null;
	
	inventory = new Array (inventoryLength);								//   Create & init the array to hold the inventory 
	for (var i : int = 0; i < inventory.length; i++) 
	{ 
		inventory *= null;* 
  • }*
    }

function Update ()
*{ *

  • if (Input.GetKeyUp (KeyCode.I))*

  • { // If the “i” key is pressed…*

  •  openInventoryWindow = !openInventoryWindow;										//	... toggle the inventory window.*
    
  • }*
    }

function OnGUI ()
{

  • // Inventory Window*
  • if (openInventoryWindow) // If the “open inventory window” toggle is true*
  • { *
  •  GUI.Window (1, inventoryWindow, DrawInventoryWindow, "Inventory");	//   The title of this window could be passed as a String from the LootableItem*
    
  • }*
    }

function DrawInventoryWindow () // The window function to draw the inventory window
*{ *

  • if (GUI.Button (Rect (5,5,10,10), “”)) // Left upper corner quit button*

  • {*

  •  CloseInventoryWindow ();*
    
  • }*

  • var j : int;*

  • var k : int;*

  • var currentInventoryItem : InventoryItem; // Establish a variable to hold our data*

  • var currentRect : Rect;*

  • for (var i : int = 0; i < inventory.length; i ++) { // Go through each row …*

  •  j = i / inventoryWidth;												//   ... divide by array by width to get rows...*
    
  •  k = i % inventoryWidth;												//   ... find the remainder by width to get columns...*
    

_ currentInventoryItem = inventory*; // … set this point in the matrix as our current point …_
_ currentRect = (new Rect (offSet.x + k * (iconWidthHeight + spacing), offSet.y + j * (iconWidthHeight + spacing), iconWidthHeight, iconWidthHeight));_
_
if (currentInventoryItem == null) // … if there is no item in the j-th row and the k-th column, draw a blank texture*_
* { *
* GUI.DrawTexture (currentRect, emptySlot);*
* }*
* else*
* {*
* GUI.DrawTexture (currentRect, currentInventoryItem.itemIcon);*
* }*

* // If there is an item at this location and there is a button click…*
* if (currentInventoryItem != null && GUI.Button (currentRect, “”, GUIStyle (“label”)))*
* {*
* if (Input.GetMouseButtonUp (0)) // … if that click is mouse button 0: see the description*
* { *
* GUIContent (" " + currentInventoryItem.itemDescription); // Get the description out*
* }*

* }*
* }*
}

function CloseInventoryWindow ()
{
* openInventoryWindow = false;*
}

function AddItem (item : InventoryItem)
{
* for (var i : int = 0; i < inventory.length; i ++) // Go through each row*
* { *
_ if (inventory == null) // If the position is empty…
* {
inventory = item; // … add the new item…
return (true); // … and exit the function.
}
}
Debug.Log (“Inventory is full”);
return (false);
}*_

function ResizeInventory (newInventoryLength) // This code is never called at this point, but can be when you integrate it.
*{ *
* var oldInventory : InventoryItem[] = inventory;*

* inventory = new Array (newInventoryLength);*
* for (var i : int = 0; i < oldInventory.length; i++)*
* {*
inventory = oldInventory*;*
* }*

* for (var j : int = oldInventory.length; j < inventory.length; j++)*
* {*
_ inventory = null;
* }
} *_