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
0
Question by ShroomWasTaken · Apr 13, 2015 at 08:51 PM · javascriptunity5

Inventory GUI help.... (click for more info)

So, i'm making a inventory system in unity using javascript. And the part where the buttons are i'm trying to make them not appear on the same spot, i want 4 in a row and then another row with 4 under. So, i made 2 var's one yOffset and the other xOffset, so everytime the for loop loops trough all the items, it makes a button, and then checks if it has looped 4 times, which is has not, so it only adds a number to the xOffset. But the buttons are still on the same spot. Why? Code :

 var inventory = Array("food", "water", "test", "All those after me, including me should be on the first row", "this should be on the 2nd row, first slot"); //The current inventory.
 
 var inventoryOpen : boolean = false; //Checks if the player opened the inventory using the "E" button.
 
 var xOffset : int = 10; //Offset of the item buttons, LEFT / RIGHT
 var yOffset : int = 10; // UP / DOWN
 
 var timesLooped : int = 0; //So the buttons dont respawn all the time.
 
 function Start()
 {
     
 }
 
 
 function Update()
 {
     if (Input.GetKeyDown(KeyCode.E) && inventoryOpen == false)
     {
         inventoryOpen = true; //Resets everything back to normal so the Inventory can eb displayed
         xOffset = 10;
         yOffset = 0;
         timesLooped = 0;
     }
     else if (Input.GetKeyDown(KeyCode.E) && inventoryOpen == true)
     {
         inventoryOpen = false;
     }
 }
 
 
 function OnGUI()
 {
     if (inventoryOpen == true)
     {
         if (timesLooped < inventory.length)
         {
             for (i in inventory)
             {
                 GUI.Button(Rect(xOffset, yOffset, 50, 50), i);
                 if (timesLooped > 1) //Makes so the first button doesn't move since its already in its spot.
                 {
                     timesLooped += 1;
                     xOffset += 10;
                     if (timesLooped > 5 && timesLooped < 6) //if theres is 4 buttons on the same row, it will add the next one on the 2nd row.
                     {
                         xOffset = 0;
                         yOffset = 60;
                     }
                     else if (timesLooped > 10 && timesLooped < 11) //same but on 3rd row.
                     {
                         xOffset = 0;
                         yOffset += 60;
                     }
                     else if (timesLooped > 20 && timesLooped < 21) //same but on 4th row.
                     {
                         xOffset = 0;
                         yOffset += 60;
                     }
                 }
             }
         }
     }
 }
 
 // Made by "HappyCrayfish" 2015.
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by WoozyBytes · Apr 14, 2015 at 05:27 PM

Hi there!!!

I think you are overcomplicating all you need is something like this:

 var numOfButtonsPerRow : int = 4;
 var numOfRows : int = 2;
 var btnWidth : int = 64;
 var btnHeight : int = 64;
 function OnGUI ()
 {
     for( var rowN = 0 ; rowN < numOfRows; rowN ++)
     {
         for( var btnN = 0 ; btnN < numOfButtonsPerRow; btnN++)
         {
             if( GUI.Button(Rect( btnN * btnWidth , rowN * btnHeight , btnWidth,btnHeight),"Button"))
             {
                 Debug.Log("Working");
             }
          }
     }
 }

Hope It Helps

Comment
Add comment · Show 3 · Share
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 ShroomWasTaken · May 11, 2015 at 01:10 PM 0
Share

I literarly forgot my password for this account until i reazlied i wrote it up (today)... Sorry for late reply. Will try this right away! Edit : So, this worked.... I can't really understand how it's not just moving ALL buttons though... Umm, could you maybe explain to me? :P Thank you for helping me :) Edit 2 : So, umm... What if i want the inventory to be an array so i can add more items and it will automatically add a new button?

avatar image WoozyBytes · May 12, 2015 at 03:17 PM 0
Share

Edit : So, this worked.... I can't really understand how it's not just moving ALL buttons though... Umm, could you maybe explain to me?

Hi @nobx101 what do you mean by moving all Buttons ?

If you mean position all buttons you can create 2 public variables that you can adjust in the inspector one for X Axis(Left/Right)and one for the Y axis(Up/Down) and add after btnWidth and after btnHeight respectively.

For Example

  var numOfButtonsPerRow : int = 4;
  var numOfRows : int = 2;
  var btnWidth : int = 64;
  var btnHeight : int = 64;
  var offSetX : int = 0; // Adjust this in inspector
  var offSetY : int = 0; // Adjust this in inspector
  function OnGUI ()
  {
      for( var rowN = 0 ; rowN < numOfRows; rowN ++)
      {
          for( var btnN = 0 ; btnN < numOfButtonsPerRow; btnN++)
          {
              if( GUI.Button(Rect( btnN * btnWidth + offSetX, rowN * btnHeight + offSetY , btnWidth,btnHeight),"Button"))
              {
                  Debug.Log("Working");
              }
          }
      }
  }
avatar image WoozyBytes · May 12, 2015 at 03:47 PM 0
Share

Edit 2 : So, umm... What if i want the inventory to be an array so i can add more items and it will automatically add a new button?

What's the max number of slots you want in the inventory?

you can set the max number of inventory slots lets say for example 20, now at first the user might have access to only 10 slots so you just disable the other 10 buttons.

 var maxSlots : int = 20; // $$anonymous$$ax Number of Slots
 var allowedNumberOfSlots : int = 10; // Size of your array
 private var curSlot : int= -1 ; // this is to count the buttons
 var numOfButtonsPerRow : int = 5; 
 var numOfRows : int = 4;
 var btnWidth : int = 64;
 var btnHeight : int = 64;
 var offSetX : int = 0; // Adjust this in inspector
 var offSetY : int = 0; // Adjust this in inspector
 function OnGUI ()
 {
     for( var rowN = 0 ; rowN < numOfRows; rowN ++)
     {
         for( var btnN = 0 ; btnN < numOfButtonsPerRow; btnN++)
         {
             curSlot++;
             if( GUI.Button(Rect( btnN * btnWidth + offSetX, rowN * btnHeight + offSetY , btnWidth,btnHeight),"Button"))
             {
                 if( curSlot < allowedNumberOfSlots )
                     Debug.Log("Working");
                 else
                     Debug.Log("Disabled");
             }
         }
          if( curSlot == maxSlots - 1  ) // $$anonymous$$us 1 because we start counting at zero
              curSlot = -1 ;
     }
 }

Is This what you need ?

if You don't want the buttons to show up when disabled change this in the code above:

 if( GUI.Button(Rect( btnN * btnWidth + offSetX, rowN * btnHeight + offSetY , btnWidth,btnHeight),"Button"))
 {
     if( curSlot < allowedNumberOfSlots )
         Debug.Log("Working");
     else
         Debug.Log("Disabled");
  }


To This:

 if( curSlot < allowedNumberOfSlots )
 {
     if( GUI.Button(Rect( btnN * btnWidth + offSetX, rowN * btnHeight + offSetY , btnWidth,btnHeight),"Button"))
     {
         Debug.Log("Working");
     }
 }

Regards

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How I can make a simple follow AI in Unity 5? 1 Answer

Monodevelop freezes opening js files after creating new project 1 Answer

Upgrading to unity 5... lots of errors... please help. 1 Answer

Simple go forward and turn back loop movement stuck to the limit without reverse 2 Answers

No animation attached to gameobject? 2 Answers


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