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

Input.GetMouseButton is not working

Discussion in 'Scripting' started by DrBibop, Feb 18, 2016.

  1. DrBibop

    DrBibop

    Joined:
    Oct 9, 2015
    Posts:
    18
    There's a problem in my script that I can't find. When trying to use Input.GetMouseButton(0), the event isn't happening. The joystick of my controller is working perfectly and Input.GetMouseButton(0) is working on my other scripts. It's really pissing me off but there is the code :

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Player : MonoBehaviour
    5. {
    6.  
    7.     public float cone;
    8.     public float health;
    9.     public bool pause;
    10.     public bool IsShooting;
    11.     public GameObject JetCollision;
    12.     public GameObject PushCollision;
    13.     public GameObject staffLight;
    14.     public Camera playerview;
    15.     public ParticleSystem Jet1;
    16.     public ParticleSystem Jet2;
    17.     public ParticleSystem Jet3;
    18.     public ParticleSystem Jet4;
    19.     public ParticleSystem Jet5;
    20.     public ParticleSystem Jet6;
    21.     public ParticleSystem Jet7;
    22.     public ParticleSystem Jet8;
    23.     public ParticleSystem Jet9;
    24.  
    25.     // Use this for initialization
    26.     void Start()
    27.     {
    28.         var J1e = Jet1.emission;
    29.         var J2e = Jet2.emission;
    30.         var J3e = Jet3.emission;
    31.         var J4e = Jet4.emission;
    32.         var J5e = Jet5.emission;
    33.         var J6e = Jet6.emission;
    34.         var J7e = Jet7.emission;
    35.         var J8e = Jet8.emission;
    36.         var J9e = Jet9.emission;
    37.  
    38.         J1e.enabled = false;
    39.         J2e.enabled = false;
    40.         J3e.enabled = false;
    41.         J4e.enabled = false;
    42.         J5e.enabled = false;
    43.         J6e.enabled = false;
    44.         J7e.enabled = false;
    45.         J8e.enabled = false;
    46.         J9e.enabled = false;
    47.  
    48.         JetCollision.GetComponent<Collider>().enabled = false;
    49.  
    50.         health = 50;
    51.  
    52.         cone = 4;
    53.     }
    54.  
    55.     // Update is called once per frame
    56.     void Update()
    57.     {
    58.         var J1em = Jet1.emission;
    59.         var J2em = Jet2.emission;
    60.         var J3em = Jet3.emission;
    61.         var J4em = Jet4.emission;
    62.         var J5em = Jet5.emission;
    63.         var J6em = Jet6.emission;
    64.         var J7em = Jet7.emission;
    65.         var J8em = Jet8.emission;
    66.         var J9em = Jet9.emission;
    67.  
    68.         if (Input.GetMouseButton(0) == true || Input.GetAxis("Horizontal 2") > 0.2 || Input.GetAxis("Horizontal 2") < -0.2 || Input.GetAxis("Vertical 2") > 0.2 || Input.GetAxis("Vertical 2") < -0.2)
    69.         {
    70.             IsShooting = true;
    71.         }
    72.  
    73.         if(Input.GetMouseButtonUp(0) || Input.GetAxis("Horizontal 2") < 0.2 && Input.GetAxis("Horizontal 2") > -0.2 && Input.GetAxis("Vertical 2") < 0.2 && Input.GetAxis("Vertical 2") > -0.2)
    74.         {
    75.             if (pause == false)
    76.             {
    77.                 IsShooting = false;
    78.  
    79.                 J1em.enabled = false;
    80.                 J2em.enabled = false;
    81.                 J3em.enabled = false;
    82.                 J4em.enabled = false;
    83.                 J5em.enabled = false;
    84.                 J6em.enabled = false;
    85.                 J7em.enabled = false;
    86.                 J8em.enabled = false;
    87.                 J9em.enabled = false;
    88.  
    89.                 JetCollision.GetComponent<Collider>().enabled = false;
    90.  
    91.                 PushCollision.GetComponent<Collider>().enabled = false;
    92.  
    93.                 staffLight.transform.localPosition = new Vector3(0.72f, 0.94f, 1.04f);
    94.             }
    95.         }
    96.  
    97.         if (health < 0)
    98.             Death();
    99.  
    100.         if(IsShooting == true)
    101.         {
    102.             if (pause == false)
    103.             {
    104.                 if (cone == 0)
    105.                 {
    106.                     J1em.enabled = true;
    107.                 }
    108.  
    109.                 else if (cone == 1)
    110.                 {
    111.                     J1em.enabled = true;
    112.                     J2em.enabled = true;
    113.                     J3em.enabled = true;
    114.                 }
    115.  
    116.                 else if (cone == 2)
    117.                 {
    118.                     J1em.enabled = true;
    119.                     J2em.enabled = true;
    120.                     J3em.enabled = true;
    121.                     J4em.enabled = true;
    122.                     J5em.enabled = true;
    123.                 }
    124.  
    125.                 else if (cone == 3)
    126.                 {
    127.                     J1em.enabled = true;
    128.                     J2em.enabled = true;
    129.                     J3em.enabled = true;
    130.                     J4em.enabled = true;
    131.                     J5em.enabled = true;
    132.                     J6em.enabled = true;
    133.                     J7em.enabled = true;
    134.                 }
    135.  
    136.                 else if (cone == 4)
    137.                 {
    138.                     J1em.enabled = true;
    139.                     J2em.enabled = true;
    140.                     J3em.enabled = true;
    141.                     J4em.enabled = true;
    142.                     J5em.enabled = true;
    143.                     J6em.enabled = true;
    144.                     J7em.enabled = true;
    145.                     J8em.enabled = true;
    146.                     J9em.enabled = true;
    147.                 }
    148.  
    149.                 JetCollision.GetComponent<Collider>().enabled = true;
    150.  
    151.                 PushCollision.GetComponent<Collider>().enabled = true;
    152.  
    153.                 staffLight.transform.localPosition = new Vector3(0, 0.94f, 1.5f);
    154.             }
    155.         }
    156.     }
    157.  
    158.     void Death()
    159.     {
    160.         playerview.transform.parent = null;
    161.  
    162.         Destroy(gameObject);
    163.  
    164.         pause = true;
    165.     }
    166. }
    167.  
    It's on line 68. I need help please.
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    If you reduce it to its simplest, does it still fail to work? That is, put this right next to the if statement you have:
    Code (csharp):
    1. if (Input.GetMouseButton(0) ) {
    2. Debug.Log("Yes");
    3. }
    4. else {
    5. Debug.Log("No");
    6. }
    Does the console output switch from No to Yes when the button is held?
     
  3. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    about line 67... put this...

    Code (csharp):
    1.  
    2. Debug.Log(Input.GetMouseButton(0));
    3.  
     
  4. Cynikal

    Cynikal

    Joined:
    Oct 29, 2012
    Posts:
    122
    as others have stated, i'll add a bit.

    Try removing the == true. and doing if (Input.GetMouseButton(0)).

    Also, a helpful tip,

    further down in your code, you have if (Shooting == true), and if (pause == false).

    You could do:
    if (Shooting), and if (!pause). It cleans up the code a bit.
     
  5. DrBibop

    DrBibop

    Joined:
    Oct 9, 2015
    Posts:
    18
    Unity does detect the click. When adding a print("Detected Left Click"), the console says it, but IsShooting isn't turning to true. Its really weird.
     
  6. Zuntatos

    Zuntatos

    Joined:
    Nov 18, 2012
    Posts:
    612
    Code (CSharp):
    1. if (Input.GetMouseButton(0) == true || Input.GetAxis("Horizontal 2") > 0.2 || Input.GetAxis("Horizontal 2") < -0.2 || Input.GetAxis("Vertical 2") > 0.2 || Input.GetAxis("Vertical 2") < -0.2)
    So, basically:
    Code (CSharp):
    1. if (mouse0Up || hori < 0.2 && hori > -0.2 && vert < 0.2 && vert > -0.2)
    > and < have precedence, so:
    Code (CSharp):
    1. if (mouse0Up || (hori < 0.2) && (hori > -0.2) && (vert < 0.2) && (vert > -0.2))
    But then && has precedence over || (source)
    Code (CSharp):
    1. if ((mouse0Up || horiUp) && (horiDown && (vertiUp && vertiDown)))
    I'm quite sure this is not the way you intended it.



    Another thing, are you sure you want
    Code (CSharp):
    1. if (Input.GetMouseButton(0))
    in the first condition that makes isShooting true? Seeing as there is a Input.GetMouseButtonUp() to turn isShooting to false, you'd likely want to use
    Code (CSharp):
    1. if (Input.GetMouseButtonDown(0))
    instead. But again - this doesn't make any difference, as it'll just do isShooting = true a lot more times.



    And now for the actual bug - your code is registering the mouse down and sets isFiring to true (mouseDown || joystickActive), then registering the joystick idling and setting isFiring to false (mouseUp || joystickIdle). You'd probably want an option to toggle between mouse only and joystick only, or you distinguish between isFiring from mouse and an isFiring from joystick and then do an if (mouseIsFiring || joystickIsFiring).
     
    DrBibop likes this.
  7. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    Nice catch! I missed that completely.
     
  8. DrBibop

    DrBibop

    Joined:
    Oct 9, 2015
    Posts:
    18
    Thank you. It's finally working. Indeed the error makes sense.