- Home /
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));
and when i press a GUI Button it the scroll position vector 2 int(int the pictur above) goes to 0.
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
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
}
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).
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.
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.
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.
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.
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();
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
}
hi sorry,i changed the script but the same problem occurs.
Your answer
![](https://koobas.hobune.stream/wayback/20220613162841im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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