Item and Inventory for a RPG game

I created a single script to be place on every gameobject tagged as item and compare the name of the gameobject and assign a value for a certain variable of the script. I’m doing this so I can have only 1 script which will identify all the items on my game.

The problem is its not working can anyone help me?

here is the script

function Update ()

{

if (Object.name == "Axe")

{

	id = 0001;

}

if (Object.name == "Bow")

{

	id = 0002;

}

}

Well, I would expect that you are getting a compile error on this, since there is no static name property for the Unity Object class. I think what you meant to do is ‘this.name’, as that refers to the GameObject the script is attached to. I must admit I’m failing to see the point of the script from this tiny segment though. If you know it is a “Bow” at design time, what’s the problem with setting the ID to 2 at design time?

How about we do this in a different way.

Define a class- ‘Item’, like so-

class Item {
    var name : string = "";
    var id : int = -1;
}

Then, make a script with nothing on it but an ‘Item’ var-

var myItem : Item;

Then, when you assign this in the editor, set up its name and id manually. You only really need to do this once for each item type, because after that you can use prefabs and duplication to make the rest.

Then, when you pick up the item, given that you have it’s gameObject, do this-

var pickedUpItem : Item = gameObject.whateverYouCalledTheScriptWithTheItemInIt.myItem;

Then, you can add it to an ArrayList of Items, or whatever, to use as your inventory.

(Disclaimer- I’m not good at javascript, since I prefer to program in C#. If anyone who is better at it than I am can see any big problems with this, tell me please!)