Button Script

I have a script in my game that requires a button to be pressed to be able to complete the level. However, it currently doesn’t work. There are no errors, but the button doesn’t have to be pressed to complete the level.

Here is the script to change scenes/finish level:

public var sceneName : String;
static var beenPressed = false;


function OnTriggerEnter (Collider) {
    if (Button.isPressed == true) {
        Application.LoadLevel(sceneName);
        beenPressed = true;
    }
}

Here is the button script:

static var isPressed = false;


function OnTriggerEnter (Collider) {
isPressed = true;
}


function Update () {
 if (LevelEndButton.beenPressed == true) {
     isPressed = false;
}
}

Can anyone spot what I’ve done wrong?

Use:

Input.GetButton(“Finish Level”) instead? this will show a button when the trigger is entered and return true if the button is clicked. if you put this in an if statement it would be if (Input.GetButton(Finish Level)) Application.LoadLevel(sceneName);

Chrrust, Didn’t you mean GUI.Button ? Or maybe you didn’t, but then it won’t show any button, it’s intended for an input button you setup from keyboard, gamepad, mouse etc.

I suggest you disable the script and have the ending trigger enable it, then make the call to application.loadlevel in the Update() or OnGUI() once you have the input you fancy.

public var sceneName : String;

function OnTriggerEnter (Collider)
{
    enabled = true;
}

function Start()
{
    enabled = false;
}

function Update()
{
    // Don't forget to put a well set button there, like Fire1 etc http://docs.unity3d.com/Documentation/ScriptReference/Input.html
    if (Input.GetButton("Your button")) 
        Application.LoadLevel(sceneName);
}

And if you meant to have a GUI button to be clicked by the player, replace the Update with this OnGUI:

function OnGUI()
{
    if (GUILayout.Button("Click me to go to the next level"))
        Application.LoadLevel(sceneName);
}

yeah, i meant Input.GetButton(“any button”) or GetKey, my bad

Did either of you read his post?
This has nothing to do with GUI buttons or key presses

to OP: Is there more than one instance of the button trigger/script? Statics will screw you if there is

also, The update test in the button script will probably never evaluate true, because once the new level is laoded, the button object will have been destroyed (unless you have dontdestroyonload)

Now that you mention it I understand that he meant an actual 3d button object in the game but sorry that it is not that obvious from the original post and sorry to try to help the guy…

OP, if you want to keep this static architecture, you need to reset the “LevelEndButton.beenPressed” var as well when you reset the button one.

Also it doesn’t need to be done in an Update(). A Start() should be sufficient to reset the variables after a new level has been loaded.

Finally, to find where the problem is, add some Debug.Log() in each OnTriggerEnter so see what happens and in what order when you finish the level.

My bet is that the level changing script shouldn’t be using a OnTriggerEnter but an update loop to check when you reach that ending button.

function Update() {
    if (Button.isPressed == true) {
        Application.LoadLevel(sceneName);
        beenPressed = true;
    }
}

I put that in because I was trying anything at that point. Turned out the scripts were fine and I just had to restart Unity. Tip to anyone reading this who is having problems with a script that they think is fine: always restart Unity before seeking help. Twice this has happened to me.