I'm new learner of Godot Mono and I couldn't understand why _UnhandledInput
method does not work. I don't get any error or warnings either.
using Godot;
using System;
public class GameScene : Node2D
{
PackedScene towerPackedScene;
public override void _Ready()
{
towerPackedScene = (PackedScene)GD.Load("res://Tower.tscn");
}
public override void _Process(float delta)
{
}
public override void _UnhandledInput(InputEvent @event)
{
if (@event is InputEventMouseButton mouseButton)
{
GD.Print("clicked"); // does not show message on output
KinematicBody2D tower = (KinematicBody2D)towerPackedScene.Instance();
tower.Position = mouseButton.Position;
this.AddChild(tower);
}
}
}
CodePudding user response:
Presumably the input is not unhandled. In other words, some other node is taking the input (for example with _input
).
You are looking for a mouse event. Is there some Control taking the mouse input? Be aware that Control
s will take mouse input before other nodes, even if they are behind them. You need to set their mouse_filter
to Ignore.
A common gotcha is using a ColorRect
or TextureRect
for background, and leave it with mouse_filter
set to Stop (which is the default), and then being unable to get input.
See Using InputEvent