Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Button Not working as intended(Solved)

Discussion in 'Scripting' started by kingbas33, Dec 30, 2014.

  1. kingbas33

    kingbas33

    Joined:
    Sep 9, 2014
    Posts:
    9
    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?

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using UnityEngine.UI;
    5. using UnityEngine.EventSystems;
    6.  
    7. public class BuildPlatform : MonoBehaviour {
    8.  
    9.     bool isClicked = false;
    10.     bool mouseInArea;
    11.     public GameObject actionMenuPrefab;
    12.     public GameObject actionMenu;
    13.     public GameObject buildMenuP1Prefab;
    14.     public GameObject buildMenuP1;
    15.     public GameObject buildMenuP2Prefab;
    16.     public GameObject buildMenuP2;
    17.  
    18.  
    19.     public Button exitButton;
    20.     public Button buildButton;
    21.     public Button backButton;
    22.     public Button nextPageButton;
    23.     public Button previousPageButton;
    24.  
    25.     // Use this for initialization
    26.     void Start () {
    27.  
    28.     }
    29.  
    30.     // Update is called once per frame
    31.     void Update () {
    32.         if (mouseInArea == true) {
    33.             if (Input.GetMouseButtonDown(0)) {
    34.                 SelectionMenu();
    35.             }
    36.         }
    37.     }
    38.  
    39.     void OnMouseEnter() {
    40.         mouseInArea = true;
    41.     }
    42.  
    43.     void OnMouseExit() {
    44.         mouseInArea = false;
    45.     }
    46.  
    47.     private void SelectionMenu(){
    48.         actionMenu = (GameObject)Instantiate (actionMenuPrefab);
    49.         GameObject A = GameObject.Find("Exit");
    50.         exitButton = A.GetComponent<Button> ();
    51.         exitButton.onClick.AddListener(() => { ExitButton();});
    52.         GameObject B = GameObject.Find ("Build");
    53.         buildButton = B.GetComponent<Button> ();
    54.         buildButton.onClick.AddListener(() => { BuildButton();});
    55.     }
    56.  
    57.     void ExitButton(){
    58.         Destroy (actionMenu);
    59.     }
    60.  
    61.     void BuildButton(){
    62.         Destroy (actionMenu);
    63.         buildMenuP1 = (GameObject)Instantiate(buildMenuP1Prefab);
    64.         GameObject B = GameObject.Find("Arrow Right");
    65.         nextPageButton = B.GetComponent<Button> ();
    66.         nextPageButton.onClick.AddListener (() => { NextPageButton();});
    67.         GameObject A = GameObject.Find ("Back");
    68.         backButton = A.GetComponent<Button> ();
    69.         backButton.onClick.AddListener(() => { BackButton(buildMenuP1);});
    70.     }
    71.  
    72.     void BackButton(GameObject CurrentPage) {
    73.         Destroy (CurrentPage);
    74.         SelectionMenu ();
    75.     }
    76.  
    77.     void NextPageButton(){
    78.         Destroy (buildMenuP1);
    79.         buildMenuP2 = (GameObject)Instantiate (buildMenuP2Prefab);
    80.         GameObject B = GameObject.Find ("Arrow Left");
    81.         previousPageButton = B.GetComponent<Button> ();
    82.         previousPageButton.onClick.AddListener (() => { PreviousPageButton();});
    83.         GameObject C = GameObject.Find ("Back");
    84.         backButton = C.GetComponent<Button> ();
    85.         backButton.onClick.AddListener(() => { BackButton(buildMenuP2);});
    86.     }
    87.  
    88.     void PreviousPageButton(){
    89.         Destroy (buildMenuP2);
    90.         backButton = null;
    91.         BuildButton ();
    92.     }
    93. }
    94.  
     
    Last edited: Jan 1, 2015
  2. REV-FX

    REV-FX

    Joined:
    Oct 24, 2013
    Posts:
    28
    Greetings!

    Your back button seems to stop working because it gets set to null in line 90 within the PreviousPageButton method. I guess you rather want to hide the button instead of nulling it.
     
  3. kingbas33

    kingbas33

    Joined:
    Sep 9, 2014
    Posts:
    9
    Thanks for trying to help but hat was one of my attempts to fix it because after that i call the Buldbutton method and in there it should find it again at line 67.