x


level selection using gui.SelectionGrid

Hi, I'm trying to create a level menu where i use a grid of button to represent the level and when i click on them it should load the particular level this is the code i'm using using UnityEngine; using System.Collections;

public class MenuScreen : MonoBehaviour{

public string selectedButton = "";

public int selectionGridInt;

public string[] selectionStrings = new string[] { "Level 1", "Level 2", "Level 3", "Level 4", "Level 5", "Level 6", "Level 7"};



void OnGUI()

{

    GUILayout.BeginArea(new Rect(350, 320, Screen.width / 3 - 20, Screen.height * 2 / 3));                                                                       

    selectionGridInt = GUILayout.SelectionGrid(selectionGridInt,selectionStrings,2);

    GUILayout.EndArea();

    selectedButton = selectionStrings[selectionGridInt];
    //Application.LoadLevel(selectedButton);
    GUI.Label(new Rect(300,10,150,40), selectedButton);

}

}

The problem is when the menu loads up the value of selectionGridInt is always 0 and therefore it loads up the first level by default instantly. There is something i'm obviously doing wrong but can't figure out.. Could someone help me out?

more ▼

asked Feb 06 '12 at 09:18 AM

noobakashi gravatar image

noobakashi
11 8 12 13

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

One possibility is to have a separate button that then loads the level based on the index.

if(GUI.Button(new Rect(300, 10, 150, 40), selectionStrings[selectionGridInt]))
{
Application.LoadLevel(selectionStrings[selectionGridInt]);
}

Another possibility is to have the first selection grid button to be "None" then just ensuring that if the selectionStrings[selectionGridInt] is None don't load the level

more ▼

answered Feb 06 '12 at 10:10 AM

CorruptedHeart gravatar image

CorruptedHeart
46 1 1 2

(comments are locked)
10|3000 characters needed characters left

You could also set the selectionGridInt in Start() to a value higher than your max. buttons. Something like this:

void Start() 
{
  selectionGridInt = selectionStrings.Length;
}
more ▼

answered Sep 02 '12 at 01:18 PM

efge gravatar image

efge
5.1k 5 14 38

(comments are locked)
10|3000 characters needed characters left

I think you can initialize selectionGridInt to -1 in Start(). Then add an if(selectionGridInt > -1) to load your level :)

more ▼

answered Feb 12 '12 at 01:20 PM

d.V.b gravatar image

d.V.b
1

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x38
x3

asked: Feb 06 '12 at 09:18 AM

Seen: 1649 times

Last Updated: Sep 02 '12 at 01:18 PM