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 samsmithnz · Jan 28, 2013 at 02:55 AM · c#buttonscreenongui

How do I create buttons dynamically in OnGui?

I'm trying to create a button for each target. The problem is my code only displays the first 2 buttons - however, I have 6 targets.

Here is my basic code. The _targetList is an array of Vector3 targets (Identified in the start event by scanning the scene for all objects with a certain "enemy" tag). What is causing my buttons to not be shown? Am I using the wrong event? I note that my console is flooded with my debug statements below: but I only need to create these buttons once (I think...)

     void OnGUI()
     {
         //Bottom right group of buttons
         print("Group created with " + _targetList.Length.ToString() + " buttons");
         GUI.BeginGroup(new Rect(Screen.width - (_targetList.Length * 100), Screen.height - 50, 200, 100));
         //Loop through each target
         for (int i = 0; i <= _targetList.Length - 1; i++)
         {
             print("Button " + (i + 1).ToString() + " created: " + new Rect((i * 100), 0, 100, 50).ToString());
             //Create a button for each target, loading the attached script with source and target coordinates
             if (GUI.Button(new Rect((i * 100), 0, 100, 50), "Shoot Target " + (i + 1).ToString()))
             {
                 StartCoroutine(ShootLaserDynamically(new Vector3(4, 1, 4), _targetList[i]));
             }
         }
         GUI.EndGroup();
 }
Comment
Add comment · Show 2
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 -hiTo- · Jan 28, 2013 at 02:48 PM 0
Share

And you're sure that _targetList.Length is 6, and not 2?

That's the only reason, afaik, why the loop would only run twice. If you collapse your debug-statements in the Unity console, do you get all 6 "Button x created: Rect(x,x,x,x)"? Or just 1 and 2?

Also, i <= _targetList.Length - 1

would be the same as

 i < _targetList.Length

if I'm not thinking backwards.

Oh, and yes, you're using the correct function. The buttons need to be drawn every frame, not just once.

avatar image samsmithnz · Jan 28, 2013 at 02:56 PM 0
Share

Thanks. _targetList is definitely 6 and I definitely get all 6 button created debug statements, but then only 2 buttons show on my screen.

The code you displayed will work as well, I'm just used to using <= in for statements. Any other ideas?

1 Reply

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

Answer by -hiTo- · Jan 28, 2013 at 03:11 PM

Yes, of course... It's your GUI Group. It's set to be only 200 pixels wide. And when every button is 100 pixels wide, you can only fit 2.

You need to change

 GUI.BeginGroup(new Rect(Screen.width - (_targetList.Length * 100), Screen.height - 50, 200, 100));

to

 GUI.BeginGroup(new Rect(Screen.width - (_targetList.Length * 100), Screen.height - 50, (_targetList.Length * 100), 100));
Comment
Add comment · Show 2 · 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 samsmithnz · Jan 28, 2013 at 03:19 PM 0
Share

That's the one. if you make that an answer, I'll mark it as correct.

Cheers mate!

avatar image Berenger · Jan 28, 2013 at 04:36 PM 1
Share

Also, you should use GUILayout ins$$anonymous$$d. It's easier to center, scale etc your gui elements that way.

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

11 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

Related Questions

Calling functions from OnGUI 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

AddListener to OnPointerDown of Button instead of onClick 4 Answers

How to make game recognise Screen resolution on startup 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