Search Unity

Button needs to be pressed twice to be used PLZ HELP

Discussion in 'Scripting' started by Robster95, Apr 28, 2020.

  1. Robster95

    Robster95

    Joined:
    Jul 3, 2019
    Posts:
    154
    I'm working on a simple main menu screen and when the scene first loads i need to press any of the buttons (start, options, or credits) twice each and then it would work and if i go back to the main menu from one of the other UI (not seperate scenes) than i'm able to press the button on

    for example I press start twice then go to the level select screen. If i press back to go to the main menu then i only need to press the start button once in order to go back to level select but the options and credit buttons still need to be pressed twice in order to go to those screens since they havent been pressed already.

    Please help i'm doing this for a school project and would appreciate any help!!!

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class SceneChange : MonoBehaviour
    7. {
    8.     public GameObject mainMenuCanvas;
    9.     public GameObject levelCanvas;
    10.     public GameObject optionsCanvas;
    11.     public GameObject creditsCanvas;
    12.  
    13.  
    14.     private void Start()
    15.     {
    16.         mainMenuCanvas.SetActive(true);
    17.         levelCanvas.SetActive(false);
    18.         optionsCanvas.SetActive(false);
    19.         creditsCanvas.SetActive(false);
    20.     }
    21.  
    22.  
    23.  
    24.     public void PressStart()
    25.     {
    26.         mainMenuCanvas.SetActive(false);
    27.         levelCanvas.SetActive(true);
    28.     }
    29.  
    30.  
    31.  
    32.     public void PressOptions()
    33.     {
    34.         optionsCanvas.SetActive(true);
    35.         mainMenuCanvas.SetActive(false);
    36.     }
    37.  
    38.  
    39.  
    40.     public void PressCredits()
    41.     {
    42.         creditsCanvas.SetActive(true);
    43.         mainMenuCanvas.SetActive(false);
    44.     }
    45.  
    46.  
    47.     public void PressBack()
    48.     {
    49.         if(levelCanvas)
    50.         {
    51.             mainMenuCanvas.SetActive(true);
    52.             levelCanvas.SetActive(false);
    53.         }
    54.  
    55.         if(optionsCanvas)
    56.         {
    57.             mainMenuCanvas.SetActive(true);
    58.             optionsCanvas.SetActive(false);
    59.         }
    60.  
    61.         if(creditsCanvas)
    62.         {
    63.             mainMenuCanvas.SetActive(true);
    64.             creditsCanvas.SetActive(false);
    65.         }
    66.     }
    67. }
    68.  
     
  2. Bender_R

    Bender_R

    Joined:
    May 21, 2018
    Posts:
    115
    The problem doesn't seem to be in this script. Looks ok. Where are the clicks coming from? Screenshot?
     
    Joe-Censored likes this.
  3. Robster95

    Robster95

    Joined:
    Jul 3, 2019
    Posts:
    154
    Sorry about switching through everything fairly quickly. I had to record this about 5 different times xD I showed the proble as well as everything in the hierarchy and their inspectors.
     

    Attached Files:

  4. Bender_R

    Bender_R

    Joined:
    May 21, 2018
    Posts:
    115
    Ok, can you share the LevelCanvas script, where you handle the click?
     
  5. Robster95

    Robster95

    Joined:
    Jul 3, 2019
    Posts:
    154
    sure here you go
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.SceneManagement;
    4.  
    5. public class LevelSelect : MonoBehaviour
    6. {
    7.     public void LoadLevel(string levelName)
    8.     {
    9.         SceneManager.LoadScene(levelName);
    10.     }
    11. }
    12.  
    something I just noticed when trying it again right now is when the game is running and I havent pressed anything it looks normal. however when I click the start options or credit button. In the inspector under rect transform in the small space above the square box and the pos x y and z tabs a text pops up saying "some values driven by canvas". this appears on all of the canvas' after i press the corresponding button on the main menu then when I press it again it moves to the next UI screen.
     

    Attached Files:

  6. Bender_R

    Bender_R

    Joined:
    May 21, 2018
    Posts:
    115
    This piece is from LevelSelect. If I'm not mistaken (screen is a bit blurry) you also have a LevenCanvas where the click is handled:

    upload_2020-4-30_20-17-17.png

    This is not an issue, this is because some of the alignment is inherited from parent.
     
  7. Robster95

    Robster95

    Joined:
    Jul 3, 2019
    Posts:
    154
    I am confused on what you're asking about. would that code affect the other UI that makes it have to click twice before getting to that menu?
     
  8. Olleson

    Olleson

    Joined:
    Sep 5, 2018
    Posts:
    1
    I think I'm experiencing the same problem where I have to press a button twice in order to enable a game component. The first click doesn't enable the component but the second one does. After I've done it once, the activation of the game component happens with one click until the parent of the component gets deactivated.

    Did you ever find out a solution?
     
    Durium likes this.
  9. Robster95

    Robster95

    Joined:
    Jul 3, 2019
    Posts:
    154
    Sorry for the late reply. No I have not found a solution to this problem. If you have though I would love to hear how!!
     
  10. monark

    monark

    Joined:
    May 2, 2008
    Posts:
    1,598
    What fixed it for me was I had some canvas that had higher draw orders explicitly set on them and even though they were disabled it wasn't until I added an Override sorting value to the canvas with the issue that it started working
     
    Joe-Censored likes this.