Unity3D Game Engine – Unity3D – Inventory System – Send and Search into Inventory – JavaScript

Create a Scene with:

– Main Camera

– GameController (Empty game object), attach:
– GameControllerScript.js
– Inventory.js

– Cube, Sphere, Cylinder, attach:
– SendToInventory.js

Inventory.js


#pragma strict

var inventory = new ArrayList(); // declare the inventory ArrayList

function Start () {
}// END Start()

function Update () {	
}// END Update()


GameControllerScript.js


#pragma strict

var displayInventory: GUIText; // Assign in Inspector

private var inventoryScript : Inventory; // variable to store another script
 
function Awake ()
{
    inventoryScript = GetComponent(Inventory); // get script 'Inventory'  
}// END Awake()

function Start(){
}// END Start()

function Update(){
	displayInventory.text = "Inventory: " + Array(inventoryScript.inventory).ToString();//display inventory by converting it to an array
}// END Update()


SendToInventory.js


#pragma strict

var myName : String;// to store the object name

private var inventory : Inventory; // store the GameController>Inventory component

function Start () {

	myName = this.gameObject.name; // get the object name
	Debug.Log(myName);// show the name of this object
	
	// inserisco in una variabile l'oggetto con nome GameController, lo slash serve perchè non ha parent
    var gameControllerObject : GameObject = GameObject.Find ("/GameController");
    // se l'oggetto esiste lo inserisco in una variabile
    if (gameControllerObject != null)
    {
        inventory = gameControllerObject.GetComponent (Inventory);
    }
    // se l'oggetto non esiste restituisce un messaggio di errore
    if (gameControllerObject == null)
    {
        Debug.Log ("Cannot find 'Inventory' script");
    }
	
}// END Start()

function Update () {
}// END Update()

function OnMouseDown ()
{
        inventory.inventory.Add(myName); // GameController>Inventory component.inventoryArrayList().Add(game object name)
        Destroy(gameObject); // destroy the game object because I have already sent it to the inventory
}// END OnMouseDown()

Play, click over Sphere, Cube, Cylinder, the inventory will be updated.

For italian peolple: come funziona?

1. Inventory.js
– dichiaro Array List(), il mio inventario

2. GameControllerScript.js
– ottiene il componente Inventory.js
– visualizza il contenuto dell’inventario

3. SendToInventory.js
– ottiene il componente Inventory.js
– al click del mouse sull’oggetto, invia il nome dell’oggetto corrente all’inventario
– distrugge l’oggetto appena inserito nell’inventario, per non rischiare di inserirlo due volte. Volendo si può anche semplicemente disabilitare il Collider per evitare la registrazione di altri click.

Search inside ArrayList

Change GameControllerScript.js:


#pragma strict

var displayInventory: GUIText; // Assign in Inspector

private var inventoryScript : Inventory; // variable to store another script
 
function Awake ()
{
    inventoryScript = GetComponent(Inventory); // get script 'Inventory'  
}// END Awake()

function Start(){
}// END Start()

function Update(){
	displayInventory.text = "Inventory: " + Array(inventoryScript.inventory).ToString();//display inventory by converting it to an array

InventorySearch();
}// END Update()

function InventorySearch(){   
    
    // search string START ---------------- 
    var howBig = inventoryScript.inventory.Count;
    var n : int = 0;
	for (n = 0; n < howBig; n++){
	   if ( inventoryScript.inventory[n] == "Cube" )
	       Debug.Log("Find Cube");
	}
    // search string END ------------------
	
}// END InventorySearch()


Conver ArrayList to Array and Search inside Array

Change GameControllerScript.js:


#pragma strict

var displayInventory: GUIText; // Assign in Inspector

private var inventoryScript : Inventory; // variable to store another script
 
function Awake ()
{
    inventoryScript = GetComponent(Inventory); // get script 'Inventory'  
}// END Awake()

function Start(){
}// END Start()

function Update(){
	displayInventory.text = "Inventory: " + Array(inventoryScript.inventory).ToString();//display inventory by converting it to an array

InventorySearch();
}// END Update()

function InventorySearch(){   
    
    // Convert ArrayList() to Array
    var newarr = new Array (Array(inventoryScript.inventory));
    Debug.Log(newarr);
    
        // search string START ---------------- 
        var n : int = 0;
	for (n = 0; n < newarr.length; n++){
	   if ( newarr[n] == "Cube" )
	       Debug.Log("Find Cube");
	}
	// search string END ------------------
	
}// END InventorySearch()


Notice:


function InventorySearch(){    
    
    // Convert ArrayList() to Array
    var newarr = new Array (Array(inventoryScript.inventory));
    Debug.Log(newarr);
    
        // search string START ---------------- 
        var n : int = 0;
	for (n = 0; n < newarr.length; n++){
	   if ( newarr[n] == "Cube" )
	       Debug.Log("Find Cube");
	}
	// search string END ------------------
	
}// END InventorySearch()