Is this a good way to script an inventory system?

Ok guys I am just wondering if this is an efficient way of implementing an inventory system.

Basically, I will be working with booleans. Say I have a boolean that is “hasSword = true” if the boolean is true, print the 2d graphic in the inventory system and give the user the ability to wield the sword for example:

function Start()
     {
      sword.gameObject.SetActiveRecursively(false);
   }
}

function Update()
     {
      if (weapon == 1 && hasSword == true)
     {
      sword.gameObject.SetActiveRecursively(true);
   }
}

Is this an efficient way of doing what I want or is there a better way?

Ok, well here is the script.

DISCLAIMER: Users may only use this code as a learning device. This work shall not be used in any commercial or non-profit product, unless given permission by the license owner (Shadonyx Entertainment). Violation of these terms may be subject to lawsuit. This is not meant to be a finished product.

Don’t plagiarize! That means no copying and saying you made it! Don’t do anything illegal! Oh, and don’t give this to random people! Just to be safe, it’s pretty much for your eyes only Nercoe. I hope other users have some discretion and decency. For some reason, I have a feeling that this will be used/plagiarize by hundreds of new users. -_-
If you avoid all that, go ahead!

There’s a few things that you should keep in mind (other than possible legal issues). This script is placed on an empty game object. Items are other game objects (with an Item script, but for this example that is irrelevant). Items that are place inside the inventory become children to the inventory and become deactivated. Vice-versa happens when you drop an item.

//Copyright 2013, Shadonyx Entertainment
//Written by Jordan Abrahams
//Strictly for learing use only! Do not plagiarize!
#pragma strict

class Inventory extends MonoBehaviour {

	var slots = new GameObject[20];
	
	var rows : int = 5;
	var collumns : int = 4;
	
	var totalWeight : float = 0.0;
	var gold : int = 0;
	
	private var player : GameObject;
	
	function Start () {
		player = GameObject.FindWithTag("Player");
	}
	
	function AddItem (obj : GameObject) {
		var item : Item = obj.GetComponent.<Item>();
		
		for(var i : int = 0; i + 1 <= slots.length; i++) {
			if(item.stack && slots*) {*

_ if(slots*.name == obj.name) {_
_ slots.GetComponent.().stackCounts += item.stackCounts;
Destroy(obj);
break;
}
}
if(!slots) {
slots = obj;
totalWeight += item.weight;
obj.transform.parent = transform;
obj.SetActive(false);*_

* var abilityArray = new Array(player.GetComponent(AbilityCaster).abilities);*
* abilityArray.Add(item.ability);*
* player.GetComponent(AbilityCaster).abilities = abilityArray;*
* break;*
* }*
* else if(i + 1 >= slots.length) {*
* Debug.Log(“Your inventory is full. Drop something to open a slot.”);*
* break;*
* }*
* }*
* }*

* function DropItem (obj : GameObject) {*
* for(var i : GameObject in slots) {*
* if(i == obj) {*
* var itemScript = i.GetComponent(Item);*
* var icon = itemScript.icon;*
* var trans = player.transform;*
* var pos = trans.position;*

* i.transform.parent = null;*
* i.transform.position = pos + trans.TransformDirection(Vector3(0, 0.5, 0.5));*

* i.SetActive(true);*

* totalWeight -= itemScript.weight;*
* if(icon) {*
* Destroy(icon);*
* }*
* i.SetActive(true);*
* i = null;*
* }*
* }*
* }*

* function MoveItem (from : int, to : int) {*
* if(slots[from]) {*
* if(slots[to]) {*
* var switchedSlot : GameObject = slots[to];*
* slots[to] = slots[from];*
* slots[from] = switchedSlot;*
* }*
* else {*
* slots[to] = slots[from];*
* slots[from] = null;*
* }*
* }*
* else {*
* Debug.LogWarning("Attempting to move a missing item; there is no item at slot: " + from);*

* }*
* }*

* function ActivateItem (item : GameObject) {*
* for(var i : GameObject in slots) {*
* if(i == item) {*
* i.SendMessage(“Activate”);*
* }*
* }*
* }*
}
I mentioned before, please remember that this is not a finished product. It is also not guaranteed to be bug free, nor to be the most efficient way. I spent many long hours working on my inventory system. Think of this as a act of empathy.

It depends on how complex inventory will be.

If there wont be wide variety of items, but only booleans if there is an item equipped, meaning that there wont be 2 or more different swords, just one sword as weapon, then you could create one InventoryController componenent, and assign it to your player.

Inside this component just set bool fields for every possible item, that will indicate if item is equipped + add methods like isSwordEquipped, isArmorEquipped etc…

Then, in your inventorty window just call those methods to check if items icon should be displayed or not.

If you need more complex equipment system, then nobody will tell you how to implement it best, if you wont describe how your equipment system should work.