4.6 U.I. Button Pres Once

Hi

At the moment I have a script that changes level when you press one of the new UI buttons. The problem I am having is that when I click on the button, it does it many times a second. (I assume this is every frame. With the new U.I. is there a way to have it so when the button is only clicked once/ only done it once until you click the button again. I have included my code as well just in case I have done something very foolish I have included my code bellow.

using UnityEngine;
using System.Collections;

public class Levels : MonoBehaviour {

	public int level = 1;
	public GameObject Level1;
	public GameObject Level2;
	public GameObject Level3;
	public GameObject Level4;

	// Use this for initialization
	void Start () {

	
	}
	
	// Update is called once per frame
	void Update () {
	
	}

	public void add (){
		if (level == 1) {
			Level1.SetActive (false);
			Level2.SetActive (true);
			Level3.SetActive (false);
			Level4.SetActive (false);
			level = level + 1; 
		}
		if (level == 2) {
			Level1.SetActive (false);
			Level2.SetActive (false);
			Level3.SetActive (true);
			Level4.SetActive (false);
			level = level + 1; 
		}
		if (level == 3) {
			Level1.SetActive (false);
			Level2.SetActive (false);
			Level3.SetActive (false);
			Level4.SetActive (true);
			level = level + 1; 
		}
	}

	public void subtract(){
		if (level == 2) {
			Level1.SetActive (true);
			Level2.SetActive (false);
			Level3.SetActive (false);
			Level4.SetActive (false);
			level = level - 1; 
		}
		if (level == 3) {
			Level1.SetActive (false);
			Level2.SetActive (true);
			Level3.SetActive (false);
			Level4.SetActive (false);
			level = level - 1; 
		}
		if (level == 4) {
			Level1.SetActive (false);
			Level2.SetActive (false);
			Level3.SetActive (true);
			Level4.SetActive (false);
			level = level - 1; 
		}
	}
}

The problem is that you add/subtract one from the “level” variable. So it just triggers the next if statement.

Take those lines out of the if statement and put it at the end of the function. :slight_smile:

Revised Code:

using UnityEngine;
using System.Collections;
public class Levels : MonoBehaviour {
	public int level = 1;
	public GameObject Level1;
	public GameObject Level2;
	public GameObject Level3;
	public GameObject Level4;
	// Use this for initialization
	void Start () {
	}
	// Update is called once per frame
	void Update () {
	}
	public void add (){
		if (level == 1) {
		Level1.SetActive (false);
		Level2.SetActive (true);
		Level3.SetActive (false);
		Level4.SetActive (false);
		}
		if (level == 2) {
		Level1.SetActive (false);
		Level2.SetActive (false);
		Level3.SetActive (true);
		Level4.SetActive (false);
		}
		if (level == 3) {
		Level1.SetActive (false);
		Level2.SetActive (false);
		Level3.SetActive (false);
		Level4.SetActive (true);
		}
		level = level + 1;
	}
	public void subtract(){
		if (level == 2) {
		Level1.SetActive (true);
		Level2.SetActive (false);
		Level3.SetActive (false);
		Level4.SetActive (false);
		}
		if (level == 3) {
		Level1.SetActive (false);
		Level2.SetActive (true);
		Level3.SetActive (false);
		Level4.SetActive (false);
		}
		if (level == 4) {
		Level1.SetActive (false);
		Level2.SetActive (false);
		Level3.SetActive (true);
		Level4.SetActive (false);
		}
		level = level - 1;
	}
}

You may also want to take a look at switch statements as they would probably better serve your purpose here.

public void add (){
if (level == 1) {
Level1.SetActive (false);
Level2.SetActive (true);
Level3.SetActive (false);
Level4.SetActive (false);
level = level + 1;
}
else if (level == 2) {
Level1.SetActive (false);
Level2.SetActive (false);
Level3.SetActive (true);
Level4.SetActive (false);
level = level + 1;
}
else if (level == 3) {
Level1.SetActive (false);
Level2.SetActive (false);
Level3.SetActive (false);
Level4.SetActive (true);
level = level + 1;
}
}

This will definitely help do similar for void sub() .