I don't seem to understand my scripts - help please

I have a separate script for each of my levels that puts a “back” and “next” button on the screen that should load the previous and next level (I’m making a storybook where each page is a level)

I have all my levels in my build.

I have an index page with 12 buttons that successfully load each of the 12 pages but the scripts that do the “back” and “next” don’t seem to work right. All back buttons go to level 1 despite the fact that each page has a unique button script that points to each subsequent previous and next.

example:

the script for page 3:

using UnityEngine;
using System.Collections;

public class buttond3:MonoBehaviour
{
	private bool m_enabled = true;

		void OnGUI()
	{
		if( m_enabled  GUI.Button( new Rect( 10, 670, 100, 50 ), "Back" ) )
	
		Application.LoadLevel( 2 );
		
				if( m_enabled  GUI.Button( new Rect( 910, 670, 100, 50 ), "Next" ) )
	
		Application.LoadLevel( 4 );
				
	}
}

For some reason, every back button goes to page 1 even though they are pointing to the integer previous to the level the button is on. The “next” buttons stop working after page 2 for some reason.

Any ideas?

All I want is a back and next button on each of my levels, which is a static image.

I would also like to bind these actions to actual actor buttons instead of overlaid GUI if someone can advise me on that.

Thanks :slight_smile:

Hi I cant tell from that why it’s not working, have you checked your build settings to make sure all your scenes are added there?

Couple of things jump out Id create one generic script that has “next level” and “previous level” specified as public variables. Then you only have script add it to each scene and just set which levels you want in the editor rather than adding a unique script for each one.

The other thing is that if each level is just an image loading a new scene is overkill just have an array of images and step through them inside one scene ?

If you really need to just flip through all levels, you could use something along the lines of this (untested):

void OnGUI ()
{
	bool enabled = GUI.enabled;
	
	GUI.enabled = enabled  Application.loadedLevel > 0;
	if (GUILayout.Button ("Previous"))
	{
		Application.LoadLevel (Application.loadedLevel - 1);
	}
	
	
	GUI.enabled = enabled  Application.loadedLevel < Application.levelCount;
	if (GUILayout.Button ("Next"))
	{
		Application.LoadLevel (Application.loadedLevel + 1);
	}
	
	GUI.enabled = enabled;
}

It should be pretty straight forward for you to modify if you don’t want to flip through all levels - leaving out for instance a menu level.

I agree with Headingwest, that script looks correct from here. Are you posting the whole thing? I would guess that if you have a whole bunch of “if (button) something” statements all in OnGui for both the Next/Prev and the 1 - 12 buttons, that maybe you messed up some brackets or parentheses and are accidentally calling the Chapter One button code when they click Prev.

One other possibility: do you have your GUI marked as “DontDestroyOnLoad” to make it persist through loading each scene? If so, that’s a mistake, because you would be overlaying a bunch of copies of the GUI on top of eachother, so maybe when you think you’re clicking Chapter 3’s “Prev” button you’re actually clicking Chapter 2’s “Prev” button because they’re both on top of each other.