Hello,
I made this inventory script for my game. It works great but has one bug…
Somtimes when i pick up an item it duplicates in my inventory…
#pragma strict
var hand : GameObject;
var sword : Texture;
var shovel : Texture;
var woodBlock : Texture;
var windowBlock : Texture;
var stoneBlock : Texture;
var nukeTex : Texture;
static var nuke : Texture;
var blank : Texture;
var house : GUISkin;
private var inventoryOpen : boolean;
private var gridValue : float = 0;
var inventoryTex : Texture;
var Grids : Texture[];
function Start () {
nuke = nukeTex;
}
function Update () {
if(Input.GetKeyUp("i")){
if(inventoryOpen == false){
inventoryOpen = true;
}
else if(inventoryOpen == true){
inventoryOpen = false;
}
}
}
function OnGUI(){
GUI.skin = house;
if(inventoryOpen == true){
GUI.BeginGroup (new Rect (0,0,360,360),inventoryTex);
//grid
gridValue = GUI.SelectionGrid(Rect(30,30,300,300), gridValue,Grids,5);
//Apply Sword
if(Grids[gridValue] == sword){
PlayerDamge.tool = 2;
hand.renderer.material.mainTexture = sword;
AddBlock.level = 1;
}
//Apply Shovel
if(Grids[gridValue] == shovel){
PlayerDamge.tool = 1;
hand.renderer.material.mainTexture = shovel;
Debug.Log("Shovel");
}
//Apply Wood Block
if(Grids[gridValue] == woodBlock){
PlayerDamge.tool = 3;
hand.renderer.material.mainTexture = woodBlock;
AddBlock.blockTex = woodBlock;
}
//Apply windowBlock
if(Grids[gridValue] == windowBlock){
PlayerDamge.tool = 3;
hand.renderer.material.mainTexture = windowBlock;
AddBlock.blockTex = windowBlock;
Debug.Log("Window");
}
//Apply stoneBlock
if(Grids[gridValue] == stoneBlock){
PlayerDamge.tool = 3;
hand.renderer.material.mainTexture = stoneBlock;
AddBlock.blockTex = stoneBlock;
}
//Apply nuke
if(Grids[gridValue] == nuke){
PlayerDamge.tool = 3;
hand.renderer.material.mainTexture = nuke;
AddBlock.blockTex = nuke;
}
GUI.EndGroup();
}
}
function OnTriggerEnter (c : Collider){
if(c.tag == "Item"){
//Sword
if(c.renderer.material.mainTexture == sword){
AddItem(sword);
Destroy(c.gameObject);
}
//Shovel
if(c.renderer.material.mainTexture == shovel){
AddItem(shovel);
Destroy(c.gameObject);
}
//Wood Block
if(c.renderer.material.mainTexture == woodBlock){
AddItem(woodBlock);
Destroy(c.gameObject);
}
//windowBlock
if(c.renderer.material.mainTexture == windowBlock){
AddItem(windowBlock);
Destroy(c.gameObject);
}
//stoneBlock
if(c.renderer.material.mainTexture == stoneBlock){
AddItem(stoneBlock);
Destroy(c.gameObject);
}
//nuke
if(c.renderer.material.mainTexture == nuke){
AddItem(nuke);
Destroy(c.gameObject);
}
}
}
function AddItem (itemTex : Texture){
var i :int;
for(i=0;i<25;i++){
if(Grids *== blank){*
_ Grids = itemTex;_
* i = 26;*
* break;*
* }*
* }*
}
Thanks!,
MileSplit