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 Albert-han · Sep 12, 2014 at 10:43 AM · scrollnotworkingbar

Scroll Bar

Hi guys,thanks for clicking and looking into this topic.

I have a problem with my script for some reason when i use this code.

         scrollPosition = GUILayout.BeginScrollView(scrollPosition, GUILayout.Width(100), GUILayout.Height(100));

alt text

and when i press a GUI Button it the scroll position vector 2 int(int the pictur above) goes to 0.

alt text

I want it to have a scroll bar like in here

This is my whole script

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class MainMenu : MonoBehaviour {
     public static MainMenu instance;
     public string CurMenu;
     public List<Buyable> Characters = new List<Buyable> ();
 
 
     public int shownumber;
     public int maxcharacters;
     public int mincharacters;
 
 
 
     public Texture2D P;
     public Texture2D S;
     public Texture2D Set;
 
 
     public Vector2 scrollPosition;
 
 
 
 
      
     
 
 
     void Awake()
     {
         CurMenu = "Main";
         instance = this;
 
     }
 
 
 
 
     // Use this for initialization
     void ToMenu(string menu){
         CurMenu = menu;
     }
     void OnGUI(){
         if (CurMenu == "Play")
             Play();
         if (CurMenu == "Shop")
             Shop();
         if (CurMenu == "Settings")
             Settings();
         if (CurMenu == "Main")
             Main();
         
     }
 
     private void Main()
     {
         if (GUI.Button (new Rect (100,150, 150, 100), P)) {
             ToMenu ("Play");
         }
         
         if (GUI.Button (new Rect (100,250, 150, 20), "Play")) {
             ToMenu ("Play");
         }
         if (GUI.Button (new Rect (400,150, 150, 100), S)) {
             ToMenu ("Shop");
         }
         
         if (GUI.Button (new Rect (400,250, 150, 20), "Shop")) {
             ToMenu ("Shop");
         }
         if (GUI.Button (new Rect (700,150, 150, 100), Set)) {
             ToMenu ("Settings");
         }
         
         if (GUI.Button (new Rect (700,250, 150, 20), "Settings")) {
             ToMenu ("Settings");
         }
     }
 
 
 
 
 
 
     private void Menu()
     {
         if(GUI.Button(new Rect(0,375,128,32),"Back")){
             ToMenu("Main");
         }
     }
 
     private void Shop()
     {
     if(GUI.Button(new Rect(0,375,128,32),"Back"))
     {
     ToMenu("Main");
     }
         if(GUI.Button(new Rect(100,50,128,32),">"))
         {
             shownumber += 1;
         }
         if(GUI.Button(new Rect(372,50,128,32),"<"))
         {
             shownumber -= 1;
         }
             foreach(Buyable buy in Characters)
             {
                 if (buy.number == shownumber)
                 {
                 if (GUI.Button (new Rect (100,100, 400, 200), buy.CharTexture)) 
                     {
                     buy.Unlocked = true;
                     }
                     
                 if (GUI.Button (new Rect (100,300, 400, 20), buy.name)) 
                     {
                     buy.Unlocked = true;
                     }
                 }
             }
         if (shownumber > maxcharacters)
         {
             shownumber = maxcharacters;
         }
         if (shownumber < mincharacters)
         {
             shownumber = mincharacters;
         }
     }
     private void Settings()
     {
         if(GUI.Button(new Rect(0,375,128,32),"Back")){
             ToMenu("Main");
         }
     }
     private void Play()
     {
         if(GUI.Button(new Rect(0,375,128,32),"Back"))
         {
             ToMenu("Main");
         }
         scrollPosition = GUILayout.BeginScrollView(scrollPosition, GUILayout.Width(100), GUILayout.Height(100));
         foreach (Buyable unlocked in Characters)
         {
             if (unlocked.Unlocked == true)
             {
                 //if(GUILayout.Button (unlocked.name));
                 if (GUILayout.Button(unlocked.CharTexture))
                     Debug.Log("Clicked the image");
                 
                 if (GUILayout.Button(unlocked.name))
                     Debug.Log("Clicked Button");
                 GUILayout.EndScrollView();
             }
 
 
         }
 
     }
 }
 [System.Serializable]
     public class Buyable
     {
         public string name;
         public Texture2D CharTexture;
         public int Cost;
         public int number;
         public bool Unlocked;
     }
 
 
 
 

Mainly this part only.

 private void Play()
     {
         if(GUI.Button(new Rect(0,375,128,32),"Back"))
         {
             ToMenu("Main");
         }
         scrollPosition = GUILayout.BeginScrollView(scrollPosition, GUILayout.Width(100), GUILayout.Height(100));
         foreach (Buyable unlocked in Characters)
         {
             if (unlocked.Unlocked == true)
             {
                 //if(GUILayout.Button (unlocked.name));
                 if (GUILayout.Button(unlocked.CharTexture))
                     Debug.Log("Clicked the image");
                 
                 if (GUILayout.Button(unlocked.name))
                     Debug.Log("Clicked Button");
                 GUILayout.EndScrollView();
             }
 
 
         }
 
     }

I tried fixing this problem myself but i couldnt find any errors and i have no idea why the scrollbar wont show up.Thanks

scrollfailposition.png (3.1 kB)
scroll.png (3.9 kB)
Comment
Add comment · Show 4
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 VesuvianPrime · Sep 12, 2014 at 11:50 AM 1
Share

Hey Albert

Sorry this isn't a solution to your problem, but I thought I would suggest the use of enums ins$$anonymous$$d of string types.

For example:

 public enum $$anonymous$$enuStates
 {
     $$anonymous$$ain,
     Play,
     Shop,
     Settings
 }


avatar image Albert-han · Sep 12, 2014 at 12:07 PM 0
Share

Hi thanks ,but whats the difference?

avatar image orb · Sep 12, 2014 at 12:31 PM 1
Share

Performance. It's both faster to check a number over a string, and avoids potentially causing some garbage for later collection (string handling in Unity has a tendency towards that with certain string comparisons).

avatar image VesuvianPrime · Sep 12, 2014 at 12:51 PM 1
Share

The performance is going to be almost negligable. The advantage of using enums is that it's much easier to change the name of a state, or add new states. Your compiler will catch any errors (like typos) before runtime.

99% of the time you should NEVER be comparing strings. If you're using strings to store states (i.e. the current menu) then you're doing something wrong.

Oh, you should also check out switches. You're essentially trying to turn OnGUI into a switch without realizing it.

1 Reply

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

Answer by orb · Sep 12, 2014 at 12:33 PM

It looks like you call GUILayout.EndScrollView() prematurely - every time a button is generated. Move it outside the button creation block.

Comment
Add comment · Show 6 · 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 Albert-han · Sep 12, 2014 at 01:13 PM 0
Share

Please correct me if im wrong but why is it added every time a button is generated.I added it before (foreach) so shouldnt it be one scroll bar for everything that (foreach) generates? If i add it after the foreach wouldnt it generate a scroll bar for every (foreach generated)thanks for answering btw.

avatar image orb · Sep 12, 2014 at 01:56 PM 0
Share

A ScrollView is a control that contains several things. Begin it once, then end it once. Your code has the end within a foreach, so it'll spit out one per button.

avatar image Albert-han · Sep 12, 2014 at 02:34 PM 0
Share

i dont really get it but here's what i think you meant.Is it?sorry if im wrong

     private void Play()
     {
         if(GUI.Button(new Rect(0,375,128,32),"Back"))
         {
             To$$anonymous$$enu("$$anonymous$$ain");
         }
         //scrollPosition = GUILayout.BeginScrollView (scrollPosition, GUILayout.Width (100), GUILayout.Height (100));
         foreach (Buyable unlocked in Characters)
         {
             if (unlocked.Unlocked == true)
             {
                 //if(GUILayout.Button (unlocked.name));
                 if (GUILayout.Button(unlocked.CharTexture))
                     Debug.Log("Clicked the image");
                 
                 if (GUILayout.Button(unlocked.name))
                     Debug.Log("Clicked Button");
             }
         }
         //GUILayout.EndScrollView();
avatar image orb · Sep 12, 2014 at 04:51 PM 0
Share

No. $$anonymous$$ore like this:

 private void Play()
 {
     if(GUI.Button(new Rect(0,375,128,32),"Back"))
     {
         To$$anonymous$$enu("$$anonymous$$ain");
     }
     scrollPosition = GUILayout.BeginScrollView(scrollPosition, GUILayout.Width(100), GUILayout.Height(100));
     foreach (Buyable unlocked in Characters)
     {
         if (unlocked.Unlocked == true)
         {
             if (GUILayout.Button(unlocked.CharTexture))
                 Debug.Log("Clicked the image");
  
             if (GUILayout.Button(unlocked.name))
                 Debug.Log("Clicked Button");
         }
     }
     GUILayout.EndScrollView(); // This line was in the wrong place
 }
 
avatar image Albert-han · Sep 13, 2014 at 06:42 AM 0
Share

hi sorry,i changed the script but the same problem occurs.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

splatPrototypes not changing in exported game 0 Answers

WaitForSeconds not working in while 2 Answers

Mobile input is not working ... 1 Answer

ScrollBar on for loops 0 Answers

My Grenade isnt working, help! 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