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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

UI Button Issues - Conflicting Scripts

Discussion in 'Scripting' started by Venture87, Jan 9, 2016.

  1. Venture87

    Venture87

    Joined:
    Dec 22, 2014
    Posts:
    14
    Dear Community,

    I have given this my best shot but am stuck at the below point. I have an interactable button with the below script attached which returns from the game scene to the main menu and the button works fine on a standalone basis. When I introduce another script which revives my character on GetMouseButtonDown that script takes preference over the button script and the button essentially stops working. Based on several other posts I thought the IPC and OnPointerClick would fix the issue but I was proven wrong.

    Code (CSharp):
    1. public class MG1ExitButton : MonoBehaviour, IPointerClickHandler {
    2.  
    3.     public void OnPointerClick(PointerEventData eventData) {
    4.         SceneManager.LoadScene(0);
    5.     }
    6. }
    Any help or pointers in the right direction would be much appreciated!

    Thanks.
    Best,
    David
     
  2. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    you shouldn't have one button do two different things at the same time. You should separate buttons or you need to rethink your logic.
     
    Venture87 likes this.
  3. Venture87

    Venture87

    Joined:
    Dec 22, 2014
    Posts:
    14
    Apologies for the confusion. The button is only meant to return to the main menu which it also does when the player is disabled. There is a different script of the player which revives the character when dead on LMB. However, when the player is activated LMB recognised the script on the player and ignores the functionality of the button script.
     
  4. Notter

    Notter

    Joined:
    Mar 8, 2015
    Posts:
    65
    What else happens when the player is revived?

    if you're just checking in some script Update for MouseButtonDown, it shouldn't disable other buttons.
    maybe when a player in revived, you have some other code the screws with it.
    (also make sure you still have EventSystem in the scene)
     
    Venture87 likes this.
  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  6. Venture87

    Venture87

    Joined:
    Dec 22, 2014
    Posts:
    14
    Thanks all for the replies. I have essentially used the third approach of the video in the first place to get to where I am now. The EventSystem is still in the Hierarchy.

    On the Player, I have the following script.
    Code (CSharp):
    1.     void Update() {
    2.         if (dead) {
    3.  
    4.             deathCooldown -= Time.deltaTime;
    5.             deathScreen.enabled = true;
    6.  
    7.             if (deathCooldown <= 0) {
    8.                 if (Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0)) {
    9.                     SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    10.                 }
    11.             }
    12.         }
    13.     }
    On the Button, I have the following script.

    Code (CSharp):
    1. public class MG1ExitButton : MonoBehaviour, IPointerClickHandler {
    2.     public void OnPointerClick(PointerEventData eventData) {
    3.         SceneManager.LoadScene(0);
    4.     }
    5. }
    When I comment out (//) the if(deathCooldown) part of the Player script, they Button works and returns me to the Main Menu. When I don't have it commented out, I am essentially revived instead of returned to the Main Menu when I click the button.
     
  7. Notter

    Notter

    Joined:
    Mar 8, 2015
    Posts:
    65
    Hmm, it seems like some kinda "race condition" on which LoadScene is called first..

    usually in games when you wanna restart, you either have a dedicated button on the screen, or a specific key, like "R".
    it might be a better option to remove the option to revive with a mouseclick
     
  8. Venture87

    Venture87

    Joined:
    Dec 22, 2014
    Posts:
    14
    Thanks Notter. Took me some time to find the time and also fix the issue. I have taken your advise and have implemented two dedicated buttons for restart and return to main menu which has resolved the issue. However, when implementing the Pause Button I encounter the very same issue. This threat has helped me fix my code somewhat (http://answers.unity3d.com/questions/665777/c-one-click-and-pauses-the-game-and-makes-player-j.html) but I still have the problem that when I click the Pause Button my Character also jumps (I want to build this for mobile so need to keep the Input on Left Mouse Button). The Wait For Seconds in the code works when Un-Pausing the game but I am also looking for a way that the Pause Button takes preference over the player script. I have posted my script below. Please do let me know if you have any other ideas.

    Code (CSharp):
    1. public class MG1Pause : MonoBehaviour {
    2.  
    3.     public GameObject pauseButton;
    4.     public GameObject pausePanel;
    5.  
    6.     static public bool paused = false;
    7.  
    8.     void Start(){
    9.         pausePanel.SetActive(false);
    10.         if(MG1StartScreen.sawOnce){
    11.         OnUnPause();
    12.         }  
    13.     }
    14.  
    15.     public void OnPause () {
    16.  
    17.         paused = true;
    18.         MG1Movement.pauseCheck = false;
    19.  
    20.         if(paused){
    21.             pausePanel.SetActive(true);
    22.             pauseButton.SetActive(false);
    23.  
    24.             Time.timeScale = 0;
    25.         }
    26.     }
    27.  
    28.     public void OnUnPause () {
    29.         pausePanel.SetActive(false);
    30.         pauseButton.SetActive(true);
    31.  
    32.         Time.timeScale = 1;
    33.         StartCoroutine (pause());
    34.     }
    35.  
    36.     IEnumerator pause(){
    37.         yield return new WaitForSeconds(0.1f);
    38.         MG1Movement.pauseCheck = true;
    39.     }
    40. }