Get Mouse Input

Hey guys,

So i made a simple scene with 2 cubes. Now i wrote a little c# script, which should get the mouse input.
if(Input.GetMouseButton(0)) {
Debug.Log(“Left mouse button was pressed.”);
}
So far so good. But when i press play it doesnt work and i dont know why. I dont get a message on the console, when i press the left button.
Im a beginner on unity :slight_smile: Thanks for ur help :slight_smile:

Did you put the script on an object in the scene?

1 Like

Is your if statement inside the Update() method?

@Billy4184 I tried it without putting it on the scene. And also put the script on one of the cubes. Sometimes it worked, but when it worked i got the message like 3-6 times…
@Whippets Yes its in the update method

That’s good. You will get the message multiple times, as you are checking if the mouse button is down every frame.
Also best to have a separate game-object with scripts on that affect the whole scene.

Can i just put the mouse script on the main camera or what do u recommend?

Normally your post-processing image effects go on the camera; but for your main scene scripts I don’t believe there are any hard and fast rules about where they go.

Personally, I have a SceneScripts game-object to put all the scripts on; unless they need to be added to a specific game-object because that’s what they control/are controlled by.

K thank you :slight_smile:
Another question:
At the start i print out “Click the blue cube”. After every click i wanna swap the message then to orange and blue and so on.
Example:
Start: “Click blue”
Click1:“Click orange”
Click2: “Click blue”
Click3: “Click orange” and so on

Do u know how to do that? :confused:

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

public class Mouse : MonoBehaviour {

// Use this for initialization
void Start () {
Debug.Log (“Click the blue cube!”);
}

// Update is called once per frame
void Update () {
if (Input.GetMouseButton (0)) {
//Debug.Log (“Left mouse button was pressed.”);
//Vector3 mousePos = Input.mousePosition;
//Debug.Log (mousePos.x);
//Debug.Log (mousePos.y);

RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast (ray, out hit, 1000f)) {
Debug.Log (“Hit!”);
} else {
Debug.Log (“Miss!”);
}
}
}
}

This is the script for now. I just track, if the mouse hit one of the 2 cubes or not. But I wanna add it that after each click it prints out, that the other cube has to be clicked.

If you want scripting help, please post in the scripting section, not general discussion.

Anyway, in this case, you want Input.GetMouseButtonDown not Input.GetMouseButton. The first one is called on every distinct click, whereas the second is called every frame that the button is found to be down (could be like 50 times for a single click).

It’s also a great idea not to ask people on the forums every time you have something you want to do. The best place for you to start is the Learn Section where you will find everything you need to know to get started with simple game mechanics.