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 lancer · Aug 12, 2013 at 09:37 PM · javascriptarraysinventory

Adding Muiltipule GUI.Buttons In an Inventory System Based On Arrays

Is there a way to, from code(JavaScript), to add line of code into my .js file?

I'm working on an inventory system and all my items are identified in an Array, I want to make it so it gets the array length and makes a GUI button for each entry in the array with the corresponding array text for the button text. I also need it so when the button gets clicked it knows what entry in the array needs to be deleted.

I need this answer as soon as possible, I'm trying to finish a system in the next day or two that needs this.

Comment
Add comment · Show 6
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 Bunnybomb7670 · Aug 12, 2013 at 09:42 PM 0
Share

just a side note, for performance management, i really suggest not having a button for each item in your inventory, they are pretty expensive for performance when you have lots on the screen

avatar image lancer · Aug 12, 2013 at 09:47 PM 0
Share

@Bunnybomb7670 Thanks for the tip, but I'm going to keep doing it with buttons.

@nixcs2512 I don't understand how to use what you wrote. What I want is for there to be a button for every item in the array. And when you click on the button, it will delete the button.

avatar image nixcs2512 · Aug 12, 2013 at 09:55 PM 0
Share

Do you need it to delete the element in your array?

avatar image lancer · Aug 12, 2013 at 09:57 PM 0
Share

yes, that too

and for the buttons to be in a grid.

avatar image nixcs2512 · Aug 12, 2013 at 10:08 PM 0
Share

Do you know how to use GUI.Button? If yes, i'm sure that my code will make a button to all items in the array. And i just edit it so you could delete the element in the array by clicking.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by nixcs2512 · Aug 12, 2013 at 09:47 PM

Use a for loop in funtion OnGUI(), you can imagine it would like:

 var array : Array[];//your array
 function OnGUI()
 {
  for(var i:int =0;i<array.Length;i++)
  {
   if(Rect,array[i].text)//Rect is the size of the button,array[i].text is the text in each element of your array
   {
    array.RemoveAt(i);
   }
  }
 }

It's just the idea, and you need to write and adjust it all by yourself.

Comment
Add comment · Show 9 · 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 lancer · Aug 12, 2013 at 10:14 PM 0
Share

Now Unity says:

 BCE0043: Unexpected token: i.
avatar image lancer · Aug 12, 2013 at 10:26 PM 0
Share

Oh, I accidentally deleted the var before I,

avatar image nixcs2512 · Aug 12, 2013 at 10:28 PM 0
Share

Edited it,just a mistake because i'm a C# programmer...

avatar image nixcs2512 · Aug 12, 2013 at 10:33 PM 0
Share

One thing more, maybe this code couldn't run,because with a normal array like int[] or float[] or Gun[] ( Gun is a class non-extended by $$anonymous$$onoBehaviour) you can't use RemoveAt.

avatar image nixcs2512 · Aug 12, 2013 at 10:43 PM 0
Share

Oh no...So you need to read more about GUI.Button, the Rect thing i mention need to be write like Rect(x,y,w,h) with x,y is the coordinate of the rectangle, w is the width and h is height, you can read more in here.

Show more comments

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

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

need to shorten my code but unsure of how 3 Answers

Does anyone know of good tutorials for learning arrays and classes? 1 Answer

Inventory Help. 2 Answers

Remove Items and Item Tooltips 0 Answers

Why does it give me this error and how can i fix it? 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