Global Variables (Javascript)

Hi all,

Im creating an inventory system for use in an RPG project. I have looked at many tutorials to give me hints into the variables I will need and have developed a script that I will apply to each item in my game, I will then set all the variables for each item in the inspector view. Here is my code:

var itemName : String;
var itemIcon : Texture2D;
var itemWeight : float;

var canBeStacked : boolean;
var maxStackLimit : int;

var canBeEquipped : boolean;
var equipArea : String;

var isMoney : boolean;
var isPotion : boolean;

var shopPurchase : int;
var shopSale : int;
var discardValue : int;

var itemModel : Transform;

var BagItem = new Array(28);

function OnMouseDown() 
{
	var item = new InventoryItem();
	item.worldObject = gameObject;
	item.itemName = itemName;
	item.itemIcon = itemIcon;
	item.itemWeight = itemWeight;
	
	if(item.canBeStacked==true)
	{
		item.maxStackLimit = maxStackLimit;
	}
	
	if(item.canBeEquipped==true)
	{
		item.equipArea = equipArea;
		isMoney = false;
		isPotion = false;
	}
	
	item.shopPurchase = shopPurchase;
	item.shopSale = shopSale;
	item.discardValue = discardValue;
	
	item.BagItem = BagItem;
	
	Inventory.statInventory.AddItem(item);
	
}

I also need to create another script for the inventory that will hold the InventoryItem class to declare all of the variables I have used as member variables of InventoryItem.

I would like to change the function that is used in the above code however, and that is where I need some help. The OnMouseDown function is very useful but due to the control system I have implemented within my game the player does not use the mouse. I would like my function to run whenever the player and item collide.

The main thing I would like to know from this topic is whether it is possible that a variable used within the script that defines my player and controls can be declared so that it can be used in other scripts within the game, (namely the itemScript). If I am able to do this I have a good idea how to implement my script effectively, however any suggestions are greatly appreciated.

Regards.

I’m not entirely sure I follow exactly what you’re asking for, but is this maybe what you’re after?

There are some examples there that might be what you’re asking for.