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 /
This question was closed Aug 12, 2015 at 02:37 PM by thornekey for the following reason:

Problem is not reproducible or outdated

avatar image
0
Question by thornekey · Apr 05, 2015 at 12:43 AM · c#guiscrollviewfor-loop

GUI wont show in for loop

In my game, Im allowing the player to input some numbers. I then want to display the numbers 1 - 10 on the screen and if any of the numbers the player chose are within 1-10 then change how they look. They player can continuously do this, each time the new set of numbers is displayed under the last. Im also trying to put it inside a scrollable gui so the player can navigate their matches. Here is my code thus far:

 void OnGUI () {
         GUI.skin = skin;
         
         noString = GUI.TextField(new Rect (10, 65, 250, 20), noString);
 
         if (GUI.Button (new Rect (Screen.width - 110, 65, 100, 20), "Add Game")) {
             givenNumbers = noString.Split(new char[] {' '});
 
             adding = true;
         
         }
 
         scrollPos = GUI.BeginScrollView(new Rect(10,110, Screen.width - 20, Screen.height - 120), scrollPos, new Rect(0, 0, 2900, 2900), true, true);    
 
             if (adding == true) {
                 
                 Debug.Log("adding new game");
                 for(int i = 0; i < givenNumbers.Length; i++) {
                     foreach (int n in Enumerable.Range (1, 40)) { 
                         if (Int32.Parse(givenNumbers[i]) != n) {
                             height += 32;
                         
                             drawNumberUnsel(width, height, n);
                     }
                         else {
                             height += 32;
                         
                             drawNumberSel(width, height, n);
                         }
                     }
                     Debug.Log(givenNumbers[i]);
                 }
                 adding = false;
 
             }
 
 
         GUI.EndScrollView();
     }
 
     public void drawNumberUnsel(float w, float h, int n) {
         Debug.Log ("in drawnumbers unsel");
         GUI.Box (new Rect (w, h, 30, 30), n.ToString(), GUI.skin.GetStyle ("unsel"));
     }
 
     public void drawNumberSel(float w, float h, int n) {
         Debug.Log ("in drawnumbers sel");
         GUI.Box (new Rect (w, h, 30, 30), n.ToString(), GUI.skin.GetStyle ("sel"));
     }


for some reason, the gui wont show on the screen.. at all. How can I fix this?

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 thornekey · Apr 06, 2015 at 03:09 AM 0
Share

bump pls help

avatar image Huacanacha · Apr 06, 2015 at 04:36 AM 0
Share

Any reason you're not using the new UI system? It's much less likely to cause you problems and is actually designed to be used in games. The old OnGUI system was more for quick and dirty prototyping or displaying debug info.

1 Reply

  • Sort: 
avatar image
0

Answer by maccabbe · Apr 06, 2015 at 04:35 AM

On line 33 you set adding to false so the gui will only be displayed for one frame. You should remove line 33. You also don't reset height so things will be drawn further down each frame, after you remove line 33 you should replace it with height=0; I would also note that displaying multiple things is exactly what the legacy gui system is bad at so your program will have low performance. Here is some code that works if you input a 1 and accept.

 using UnityEngine;
 using System;
 using System.Collections;
 
 public class NewBehaviourScript : MonoBehaviour {
     string[] givenNumbers;
     bool adding;
     Vector2 scrollPos;
     int height=30;
     int width=30;
     string noString="";
     void OnGUI() {
         noString=GUI.TextField(new Rect(10, 65, 250, 20), noString);
 
         if(GUI.Button(new Rect(Screen.width-110, 65, 100, 20), "Add Game")) {
             givenNumbers=noString.Split(new char[] { ' ' });
 
             adding=true;
 
         }
 
 
         scrollPos=GUI.BeginScrollView(new Rect(10, 110, Screen.width-20, Screen.height-120), scrollPos, new Rect(0, 0, 2900, 2900), true, true);
 
         if(adding) {
             height=0;
             for(int i=0; i<givenNumbers.Length; i++) {
                 for(int n=1; n<=40; n++) {
                     int value;
                     if(Int32.TryParse(givenNumbers[i], out value)) {
                         if(value!=n) {
                             Debug.Log(width+" "+height+" "+n);
                             height+=32;
 
                             drawNumberUnsel(width, height, n);
                         }
                         else {
                             height+=32;
 
                             drawNumberSel(width, height, n);
                         }
                     }
                 }
                 Debug.Log(givenNumbers[i]);
             }
         }
         GUI.EndScrollView();
     }
 
     public void drawNumberUnsel(float w, float h, int n) {
         Debug.Log("in drawnumbers unsel");
         GUI.Box(new Rect(w, h, 30, 30), n.ToString());
     }
 
     public void drawNumberSel(float w, float h, int n) {
         Debug.Log("in drawnumbers sel");
         GUI.Box(new Rect(w, h, 30, 30), n.ToString());
     }
 }

Comment
Add comment · Show 1 · 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 thornekey · Apr 07, 2015 at 02:57 AM 0
Share

removing the 'adding' bool kind of works but then I cant add more games to it.. how else can i call and add GUI inside a scroll window whenever the user wants without it disappearing?

Follow this Question

Answers Answers and Comments

20 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 avatar image avatar image avatar image

Related Questions

Can you have a scrollview inside a textfield? 0 Answers

Distribute terrain in zones 3 Answers

Push down GUI everytime for loop cycles through Array 1 Answer

[4.6] Add scroll wheel functionality to scroll rect 2 Answers

ScrollWindow AutoUpdating value help :( 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