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

If MouseButtonUp stop the game

Discussion in '2D' started by SamVorst, Dec 19, 2019.

  1. SamVorst

    SamVorst

    Joined:
    Nov 28, 2019
    Posts:
    63
    Hi,

    I have an issue, because i want if you release the leftclick that the player goes to the following scene. But that won't work...

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class Stop : MonoBehaviour
    7. {
    8.     public void Update()
    9.     {
    10.  
    11.         bool MouseButtonDown = true;
    12.         bool MouseButtonUp= true;
    13.  
    14.         if (MouseButtonDown == true)
    15.             print("oke");
    16.         else if (MouseButtonUp == true)
    17.             SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
    18.     }
    19. }
    20.  
    21.  
    22.  
    23.  
    24.  
    25.  
    Thanks sam
     
  2. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,353
    Hey Sam,
    You are setting those 2 bools MouseButtonDown and MouseButtonUp to true in update, which happens every frame. Then, you are checking if they are true, which will ALWAYS be true since it is calling every frame. It also appears that you are not using GetMouseButtonDown or GetMouseButtonUp properly so it isnt entirely clear what you are trying to accomplish.

    -Cornysam
     
    Last edited: Dec 19, 2019
  3. SamVorst

    SamVorst

    Joined:
    Nov 28, 2019
    Posts:
    63
    Oké thanks, how to solve it?
     
  4. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,353
    Well what are you trying to do with this script? My guess is that when GetMouseButtonUp occurs, you want the scene to change. let me know if that is close or not
     
  5. SamVorst

    SamVorst

    Joined:
    Nov 28, 2019
    Posts:
    63
    @Cornysam Yeah, that is right! I want that the player is gameover when the button is released!
     
  6. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,353
    Okay, so you need to use GetMouseButtonUp properly. You don't need to set up your own bools. Something like this is what you are after:

    Code (CSharp):
    1. void Update()
    2.     {
    3.         if (Input.GetMouseButtonUp(0))
    4.         {
    5.             Debug.Log("Pressed left click.");
    6.             Debug.Log("Here is where we do a function to end the game");
    7.         }
    8.     }
     
  7. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,353
    Test that, once you get that working, you need to determine what "ending your game" actually means. Does the timer stop and a UI panel pops up? Does the game pause and say "Victory"? Does it take you to another scene? Etc.
     
  8. SamVorst

    SamVorst

    Joined:
    Nov 28, 2019
    Posts:
    63
    @Cornysam yes, this works but there is a problem because the idea of the game is: you are in the main menu presses a button and you have to hold it, and if you release it you are gameover. And now if i in the main menu the button presses he starts only I have to click another time to stop and that is not what I want...

    Maybe because I have in the main menu a script to the button:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6.  
    7. public class Play : MonoBehaviour
    8. {
    9.     public void PlayGame()
    10.     {
    11.         SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
    12.     }
    13.  
    14.     public void Update()
    15.     {
    16.         if (Input.GetMouseButtonDown(0))
    17.             SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
    18.     }
    19. }
    And this one in the game itself:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class Stop : MonoBehaviour
    7. {
    8.     void Update()
    9.     {
    10.         if (Input.GetMouseButtonUp(0))
    11.         {
    12.             Debug.Log("Pressed left click.");
    13.             SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
    14.         }
    15.     }
    16.  
    17. }
    18.  
    19.  
    20.  
    21.  
    22.  
    23.  
     
  9. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,353
    So, if both the Play script and Stop script are on the same button, you will have issues. On Play, you change the scene when the GetMouseButtonDown so you will never reach GetMouesButtonUp because the scene changes before you can reach that part of the code.
     
  10. SamVorst

    SamVorst

    Joined:
    Nov 28, 2019
    Posts:
    63
    But it can't in only the play script because he then never stops becasue you're in an other scene...
     
  11. SamVorst

    SamVorst

    Joined:
    Nov 28, 2019
    Posts:
    63
    So any idea how to solve that in a better way?
     
  12. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,353
    Exactly what I am saying. You will need to change what occurs in your Play script since you will never reach your Stop part. The structure of your code isnt set up properly.
     
  13. SamVorst

    SamVorst

    Joined:
    Nov 28, 2019
    Posts:
    63
    right ill try
     
  14. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,353
    My biggest tip is try to avoid changing scenes if possible. You can mimic that change by enabling and disabling parts of the canvas like Panels and such. If you change scenes so much will will have to use PlayerPrefs a lot to carry over your info.
     
  15. SamVorst

    SamVorst

    Joined:
    Nov 28, 2019
    Posts:
    63
    thanks!
     
  16. SamVorst

    SamVorst

    Joined:
    Nov 28, 2019
    Posts:
    63
    @Cornysam I have added my script to the button in each scene, if I click in the main menu on the button does he it right, but if i release he stays counting score untill you press again... This is my script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6.  
    7. public class Play : MonoBehaviour
    8. {
    9.     public void StartStopGame()
    10.     {
    11.         if (Input.GetMouseButtonUp(0))
    12.             SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
    13.     }
    14.  
    15.     public void Update()
    16.     {
    17.         if (Input.GetMouseButtonDown(0))
    18.             SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
    19.     }
    20. }