- Home /
Help with Inventory Script
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[i] == blank){
Grids[i] = itemTex;
i = 26;
break;
}
}
}
Thanks!, MileSplit
What exactly do you mean by "duplicates"? Do you mean you can pick up the same item twice, because there seems to be no check for that at all anyway...
Answer by Lockstep · Feb 26, 2013 at 06:28 PM
You need to add a check if the item is already in your inventory. This could be done by modifying the AddItem function:
function AddItem (itemTex : Texture){
var i :int;
for(int j =0; j<Grids.length;j++){ //go through every item in Grids
if(Grids[j] == itemTex) //if we find the input texture already
return null; //exit the AddItem function immediately
}
for(i=0;i<25;i++){
if(Grids[i] == blank){
Grids[i] = itemTex;
i = 26; //this is obsolete by the way
break;
}
}
}
Your answer
Follow this Question
Related Questions
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Setting Scroll View Width GUILayout 1 Answer
Item pickup add to inventory? 2 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Script Not Working 0 Answers