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

Issue while detecting a mouse click

Discussion in 'Scripting' started by NamitDaCreator, Mar 9, 2021.

  1. NamitDaCreator

    NamitDaCreator

    Joined:
    Mar 6, 2021
    Posts:
    33
    I am making a game, and I want to be able to detect a mouse click. I checked the Unity Scripting API, and my code should work. To make sure that it would work, I inserted a debug command. However, when I tested it, I never got a debug message. Did I overlook anything or misspell anything? Also, I am new to forums, so if I mistagged it, or put it in the wrong sub forum, please let me know.

    My code:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class GameController : MonoBehaviour {

    void Update() {

    if (Input.GetMouseButtonDown(0)) {
    Debug.Log("Left Clicked");
    }
    }
    }
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,735
    Check to see if the if statement is being run at all - put a Debug.Log outside the if statement.

    Is the script attached to an object in the scene?
     
  3. NamitDaCreator

    NamitDaCreator

    Joined:
    Mar 6, 2021
    Posts:
    33
    I inserted a debug statement outside the if statement, and it didn't show. However, I checked, and the code is attached to an empty object.
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Verify both the script component and the GameObject it is attached to are both enabled in the scene. Add a Debug.Log statement before the "if" statement to verify Update() is even being run.
     
  5. NamitDaCreator

    NamitDaCreator

    Joined:
    Mar 6, 2021
    Posts:
    33
    I did, they are both enabled in the scene. Also, I updated the code to have another if statement. As I said, the void Update does not seem to be called.

    Code (CSharp):
    1. void Update() {
    2.         Debug.Log("Code Run");
    3.         if (Input.GetMouseButtonDown(0)) {
    4.             Debug.Log("Left Clicked");
    5.         }
    6.     }
     
  6. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Can you show a screenshot of your editor, with this object selected, and the script showing up in the GameObject's inspector? There must be something simple that is set up wrong. Update() getting called is extremely reliable when set up correctly.
     
  7. NamitDaCreator

    NamitDaCreator

    Joined:
    Mar 6, 2021
    Posts:
    33
    Screenshot (7).png Does this help?
     
  8. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Do you have any console messages when you hit Play?
     
  9. BenniKo

    BenniKo

    Joined:
    Mar 24, 2015
    Posts:
    100
    I think the eventsystem is missing.
    When you create a new scene unity adds one object called "EventSystem" to your hierachy.
    I cant's see that in your screenshot.
     
  10. NamitDaCreator

    NamitDaCreator

    Joined:
    Mar 6, 2021
    Posts:
    33
    No, I don't have any error messages.
     
  11. NamitDaCreator

    NamitDaCreator

    Joined:
    Mar 6, 2021
    Posts:
    33
    Really? I normally only get that when I add UI elements.
     
  12. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    But do you have any console messages?

    Also, are you sure this script or the GameObject is not disabled or destroyed when playing? You're not switching scenes or anything as well?
     
  13. NamitDaCreator

    NamitDaCreator

    Joined:
    Mar 6, 2021
    Posts:
    33
    My console is clear, I have no destruction of GameObjects in either of my script, and I only have one scene.
     
  14. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Can you verify your script changes are actually saved? Just click on GameController in the Project window and look at what code it shows in the inspector. Also verify you have not disabled the various message categories in the console.
     
  15. NamitDaCreator

    NamitDaCreator

    Joined:
    Mar 6, 2021
    Posts:
    33
    Yeah, that seems to be it. I simply disabled debug messages. Thanks!
     
    Joe-Censored likes this.
  16. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Glad you found it. You had me really scratching my head :p