Issue while detecting a mouse click

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”);
}
}
}

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?

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.

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.

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.

void Update() {
        Debug.Log("Code Run");
        if (Input.GetMouseButtonDown(0)) {
            Debug.Log("Left Clicked");
        }
    }

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.

6918788--811571--Screenshot (7).pngDoes this help?

Do you have any console messages when you hit Play?

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.

No, I don’t have any error messages.

Really? I normally only get that when I add UI elements.

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?

My console is clear, I have no destruction of GameObjects in either of my script, and I only have one scene.

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.

Yeah, that seems to be it. I simply disabled debug messages. Thanks!

1 Like

Glad you found it. You had me really scratching my head :stuck_out_tongue: