NGUI in Unity what code to a button to go to the next scene

3.5k Views Asked by At

I'm newbie in Unity3D, I am using NGUI and i don't know how to code a button created from NGUI button. The only code I know is:

void OnMouseDown    
Application.loadlevel(1);

But it's not working in NGUI button. Can someone help me with these simple problem? I'm sorry for asking this small problem of mine, I'm just only a student and beginner, I hope you understand! thank you in advance.

3

There are 3 best solutions below

2
On

You're going to have to use "OnClick" instead. To load the next level on a button click you'd have to add this to the script attached to the game object in question:

void OnClick ()
{
    Application.LoadLevel(Application.loadedLevel + 1);
}

Attach this to your button and it should work.

One thing you might want to do however is to get a centralized script for all your buttons and do a switch case to see which button is pressed, so you don't get a million scripts for each button in your GUI. That would get rather messy really fast!

1
On

try the following steps:

  1. Go to File-> Build Settings.
  2. Add your scene to it.
  3. A number would be shown.
  4. Either pass the number or the name of the scene in Application.Loadlevel(here).
0
On
void OnHover(bool state)
{
 Debug.Log(this.name + " Hover: " + state);
}
void OnPress(bool state)
{
 Debug.Log(this.name + " Pressed: " + state);
}
void OnClick()
{
 Debug.Log(this.name + " Clicked");
 Application.LoadLevel(Application.loadedLevel + 1);
}
void OnDrag(Vector2 delta)
{
 Debug.Log(this.name + " Drag: " + delta);
}
void OnDrop(GameObject droppedObject)
{
 Debug.Log(droppedObject.name + " dropped on " + this.name);
}
void OnSelect(bool state)
{
 Debug.Log(this.name + " Selected: " + state);
}
void OnTooltip(bool state)
{
 Debug.Log("Show " + this.name + "'s Tooltip: " + state);
}
void OnScroll(float delta)
{
 Debug.Log("Scroll of " + delta + " on " + this.name);
}