NGUI in Unity what code to a start button to go to the Second scene

4k Views Asked by At

And I am using NGUI for my project 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, i want to go to the next scene when the Start button is clicked. 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.

4

There are 4 best solutions below

0
On

For any UI elements from NGUI that need to react to user input, attach the NGUIButtonMessage script to the object. The object must have a collider in order for this script to work. In that script, you could tell it when to trigger it. Some examples are when you single click the object or when you just pressed it or released it. Then the next property of the script is the method or function name. That is the name of the method that you want to call in your script. The method name in your script must match what's on the method/function name field of the NGUIButton message. The last property is the trigger object. This is the object in which the script resides. For example if the method name that you want to handle the click in is called OnSubmitBttnClicked, and the target is the same game object which has the button message attached, there should be at least one script in that game object that contains a method with such name. Keep in mind if more than one method with the same name exist within the same object, all methods will get called. Here is an example of the method signature of the OnSubmitBttnClicked example:

private void OnSubmitBttnClicked()
{
}

You can also use

//The parameter clickedBttn is the object that contains the button message component
private void OnSubmitBttnClicked(GameObject clickedBttn)
{
}

The level of protection does not matter. You could use private public or protected and it would still get called.

Hope this helps.

0
On

Try something like this: (C# script)

using UnityEngine;
using System.Collections;

public class SceneButton: MonoBehaviour {
    public string targetScene; //Scene name

    void Start(){
        UIEventListener.Get(gameObject).onClick += OnButtonClick;
    }

    void OnButtonClick(GameObject g){
        Application.LoadLevel(targetScene);
    }
}

Remember that the attached element must be in the NGUI Root... And will need a Collider.

0
On

Simply use void OnClick(){ } instead of OnMouseDown.It will work.

0
On
void OnClick()
{
    Application.LoadLevel(1);
}