x


Issues with Inventory Code (Fixed!!)

So I'm still fairly new to scripting and Unity but I managed to piece together these scripts. Problem is, I need to put them together. The Inventory script is from a forum and it works fine. My issue is trying to take the images/boxes that appear, and put them into a Gui Inventory Window that I open using a Gui button in my game HUD. I want to figure out how to have the boxes open with that same button.

first code that is the inventory that works, but is on all the time:

    static var statInventory : Inventory;

//Our inventory
var inventory : Array;

//This will be drawn when a slot is empty
public var emptyTex : Texture;

//the size of the inventory in x and y dimension
public var inventorySizeX = 8;
public var inventorySizeY = 5;

//The pixel size (height and width) of an inventory slot
var iconWidthHeight = 20;

//Space between slots (in x and y)
var spacing = 4;

//set the position of the inventory
public var offSet = Vector2( 100, 100 );

// TEST VARIABLES
// Assign these to test adding Items
public var testTex : Texture;
public var testTex2 : Texture;

public var testInvObject : GameObject;
public var testInvObject2 : GameObject;

//Our Representation of an InventoryItem
@System.Serializable
class InventoryItem
{
   //GameObject this item refers to
   var worldObject : GameObject;
   //What the item will look like in the inventory
   var texRepresentation : Texture;
}

// Create the Inventory
function Awake()
{
    statInventory = this;

   inventory = new Array(inventorySizeX);

    for( var i = 0; i < inventory.length; i ++ )
    {
        inventory[i] = new Array(inventorySizeY);
    }
}

function OnGUI()
{
    var texToUse : Texture;
    var currentInventoryItem : InventoryItem;

     //Go through each row
     for( var i = 0; i < inventory.length; i ++ )
     {
         // and each column
         for( var k = 0; k < inventory[i].length; k ++ )
         {
             currentInventoryItem = inventory[i][k];

             //if there is an item in the i-th row and the k-th column, draw it
             if( inventory[i][k] == null )
             {
                 GUI.DrawTexture( new Rect( offSet.x+k*(iconWidthHeight+spacing), offSet.y+i*(iconWidthHeight+spacing), iconWidthHeight, iconWidthHeight ), emptyTex );
             }
             else
             {
                GUI.DrawTexture( new Rect( offSet.x+k*(iconWidthHeight+spacing), offSet.y+i*(iconWidthHeight+spacing), iconWidthHeight, iconWidthHeight ), currentInventoryItem.texRepresentation );
             }

             if(currentInventoryItem != null && 
                GUI.Button( new Rect( offSet.x+k*(iconWidthHeight+spacing), offSet.y+i*(iconWidthHeight+spacing), iconWidthHeight, iconWidthHeight ), "", GUIStyle("label") ))
             {

                currentInventoryItem.worldObject.transform.position = transform.position;
                currentInventoryItem.worldObject.transform.rotation = transform.rotation;
                currentInventoryItem.worldObject.active = true;

                if(Input.GetMouseButtonUp(0))
                {       
                    //Equip it
                    currentInventoryItem.worldObject.transform.parent = transform;

                } else if(Input.GetMouseButtonUp(1))
                {
                    //Drop it
                    inventory[i][k] = null; 
                    currentInventoryItem.worldObject.transform.parent = null;

                }
             }
         }
    }

    if( GUILayout.Button("AddItem1"))
    {
        var newInvObj = Instantiate(testInvObject, Vector3.zero, Quaternion.identity);
        newInvObj.active = false;
        AddItem( newInvObj, testTex );   
    }

    if( GUILayout.Button("AddItem2"))
    {
        var newInvObj2 = Instantiate(testInvObject2, Vector3.zero, Quaternion.identity);
        newInvObj2.active = false;
        AddItem( newInvObj2, testTex2 );      
    }   
}

function AddItem( item : InventoryItem )
{
    //Go through each row
    for( var i = 0; i < inventory.length; i ++ )
    {
        // and each column
        for( var k = 0; k < inventory[i].length; k ++ )
        {
           //If the position is empty, add the new item and exit the function
           if( inventory[i][k] == null )
            {
                inventory[i][k] = item;
                return;
            }
        }
    }   

    //If we got this far, the inventory is full, do somethign appropriate here   
}

function AddItem( worldObject : GameObject, texRep : Texture )
{
   var newItem = new InventoryItem();

   newItem.worldObject = worldObject;
   newItem.texRepresentation = texRep;

   AddItem( newItem );   
}

And my code that opens the Gui I would like my inventory to be in. (already opens a gui window.)

//Variable to show or hide screen
var Render;
//box and button texture variables for placement in Unity Inspector
var boxTexture : Texture;
var mainbtnTexture : Texture;

function OnGUI(){
    //If click on the Corner button in game
     if (GUI.Button(Rect(10,10,80,80),mainbtnTexture))
     {     //Show the inventory screen
            Render = true;
     }
    // shows these GUIs on Render
    if (Render)
    {
        GUI.Box (Rect (300,100,1200,800),boxTexture);



        if(GUI.Button (Rect(850,800,80,35), "Back"))
        {
            Render = false;
        }

    }
}

ANY help would be fantastic. Thanks!strong text

more ▼

asked Dec 01 '10 at 04:13 AM

Heather gravatar image

Heather
97 5 5 12

If you've answered your own question please post it as an answer and mark it as the accepted answer. That helps keep the Unanswered questions list full of just unanswered questions. thanks :)

Dec 02 '10 at 06:09 AM Rennat
(comments are locked)
10|3000 characters needed characters left

0 answers: sort voted first
Be the first one to answer this question
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3694
x3465
x790
x224

asked: Dec 01 '10 at 04:13 AM

Seen: 1465 times

Last Updated: Dec 01 '10 at 04:43 AM