Hello, I’m currently testing out some stuff but now I have a problem I can’t seem to figure out on my own
I’m new to programming and cannot see why this is happening.
When i run my program and click on the object it displays the ActionMenu, Here the build and exit buttons function normally. When I click on the build button i have the option to go to the next page but then the back buttons stops functioning even when i go back to the first page. This isn’t supposed to be happening and when i dont use it right away on the first page without first going to the second it does work. When i check in the inspector i say Missing(Button) but i have checked and it does actually find the button.
What have I done wrong?
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class BuildPlatform : MonoBehaviour {
bool isClicked = false;
bool mouseInArea;
public GameObject actionMenuPrefab;
public GameObject actionMenu;
public GameObject buildMenuP1Prefab;
public GameObject buildMenuP1;
public GameObject buildMenuP2Prefab;
public GameObject buildMenuP2;
public Button exitButton;
public Button buildButton;
public Button backButton;
public Button nextPageButton;
public Button previousPageButton;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (mouseInArea == true) {
if (Input.GetMouseButtonDown(0)) {
SelectionMenu();
}
}
}
void OnMouseEnter() {
mouseInArea = true;
}
void OnMouseExit() {
mouseInArea = false;
}
private void SelectionMenu(){
actionMenu = (GameObject)Instantiate (actionMenuPrefab);
GameObject A = GameObject.Find("Exit");
exitButton = A.GetComponent<Button> ();
exitButton.onClick.AddListener(() => { ExitButton();});
GameObject B = GameObject.Find ("Build");
buildButton = B.GetComponent<Button> ();
buildButton.onClick.AddListener(() => { BuildButton();});
}
void ExitButton(){
Destroy (actionMenu);
}
void BuildButton(){
Destroy (actionMenu);
buildMenuP1 = (GameObject)Instantiate(buildMenuP1Prefab);
GameObject B = GameObject.Find("Arrow Right");
nextPageButton = B.GetComponent<Button> ();
nextPageButton.onClick.AddListener (() => { NextPageButton();});
GameObject A = GameObject.Find ("Back");
backButton = A.GetComponent<Button> ();
backButton.onClick.AddListener(() => { BackButton(buildMenuP1);});
}
void BackButton(GameObject CurrentPage) {
Destroy (CurrentPage);
SelectionMenu ();
}
void NextPageButton(){
Destroy (buildMenuP1);
buildMenuP2 = (GameObject)Instantiate (buildMenuP2Prefab);
GameObject B = GameObject.Find ("Arrow Left");
previousPageButton = B.GetComponent<Button> ();
previousPageButton.onClick.AddListener (() => { PreviousPageButton();});
GameObject C = GameObject.Find ("Back");
backButton = C.GetComponent<Button> ();
backButton.onClick.AddListener(() => { BackButton(buildMenuP2);});
}
void PreviousPageButton(){
Destroy (buildMenuP2);
backButton = null;
BuildButton ();
}
}