Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Heather · Dec 01, 2010 at 04:13 AM · javascriptguibuttoninventory

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Rennat · Dec 02, 2010 at 06:09 AM 2
Share

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 :)

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Remove Items and Item Tooltips 0 Answers

Key for GUI.Button 2 Answers

Best way to create large amount of buttons 2 Answers

need to shorten my code but unsure of how 3 Answers

proper inventory system issue.. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges