Why not working func _on_YarnMouse_body_entered

Godot Version

3

my mouse

extends RigidBody2D

# Variables for ball movement and speed
export (float) var speed = 500
var velocity = Vector2()
var increase_amount = 1
var contacts: int = 0

signal score_increment

func _ready():
	# Set the initial velocity of the ball
	velocity = Vector2(speed, speed).rotated(randf() * 2 * PI)
	linear_velocity = velocity


func _physics_process(delta):
	var linear_velocity = velocity
	var new_contacts = get_colliding_bodies().size()
	if linear_velocity.length() < speed * delta:
		linear_velocity = linear_velocity.normalized * speed * delta
	if new_contacts > contacts:
		speed *= increase_amount
	contacts = new_contacts
		

	
func _on_YarnMouse_body_entered(body):
	if body.is_in_group("Paws"):
		emit_signal("score_increment")

game.gd

extends Node2D

var score = 0
onready var score_label = $Timer # Make sure to set the path to your score label node

func _ready():
	var yarn_mouse = $YarnMouse  # Adjust the path to your YarnMouse node
	yarn_mouse.connect("score_increment", self, "_on_score_increment")

func _on_score_increment():
	score += 1
	score_label.text = str(score)

I just want when my mouse collides with paws my score +1

To increment the score when the “YarnMouse” collides with “Paws,” your code is almost correct but needs minor adjustments. Ensure that

The _on_YarnMouse_body_entered(body) function is connected to the body_entered signal, and the collision logic is correct.

extends RigidBody2D

# Variables for ball movement and speed
export (float) var speed = 500
var velocity = Vector2()
var increase_amount = 1
var contacts: int = 0

signal score_increment

func _ready():
    # Set the initial velocity of the ball
    velocity = Vector2(speed, speed).rotated(randf() * 2 * PI)
    linear_velocity = velocity

func _on_YarnMouse_body_entered(body):
    if body.is_in_group("Paws"):
        emit_signal("score_increment")

In your game.gd, make sure the score_label references the correct node, and it’s updated properly when the score increments.

extends Node2D

var score = 0
onready var score_label = $ScoreLabel  # Update the path to your actual ScoreLabel node

func _ready():
    var yarn_mouse = $YarnMouse  # Adjust the path to your YarnMouse node
    yarn_mouse.connect("score_increment", self, "_on_score_increment")

func _on_score_increment():
    score += 1
    score_label.text = str(score)

I hope I fixed the script

Thanks, but i think my code was like this before :sweat_smile: :sweat_smile: :sweat_smile:

:rage:

I updated references to ScoreLabel and YarnMouse to ensure they point to the correct nodes.

AND Ensured that the score_increment signal is properly connected to the score update function in the game script.

you mean i need to change onready var score_label = $ScoreLabel to this one ? but why i need to change this if in my game i need to use $Timer

If $Timer is actually a Label node that you’re using to display the score, then you don’t need to change anything. However, if $Timer is a Timer node and not meant for displaying the score, you’ll need to update the reference to point to the correct node that shows the score, like this:

onready var score_label = $Path/To/Your/ScoreLabel

It’s important to make sure you’re referencing the right node for updating the score. If $Timer is managing time and not the score display, you should update the path to the correct score label node instead.

my $Timer is an label,And i think its right node


My game tree.

Some basic debugging:

  • Print some text from the _on_YarnMouse_body_entered function
  • Print some different text from the _on_score_increment function

Run the game and see what does and doesn’t get printed - that will tell you if either of those functions are actually getting called.

1 Like

I added and there is nothing in my console

Ok, so none of them are getting called! How are you connecting the body_entered signal to the _on_YarnMouse_body_entered function? Perhaps that’s where the problem is.

with inspector

Huh, that looks just fine. When you tested, you did try to hit the yarn mouse with the paw, right?

nope i hit it, i tried again same, nothing in my debuger

Hmm. Some other things to check:

  • The spelling of the “Paws” group, including that it should be an upper case P
  • In Godot 4, rigidbodies do not by default emit signals when something collides with them - you need to enable contact monitoring:
    image
    I don’t know if that’s also the case in Godot 3, but worth checking, and if such a checkbox exists, making sure that it’s enabled.
1 Like

my cat paws is kinematicbody maybe thats the prtoblem ?

yes i am using contact monitor

Try also increasing Contacts Reported to something like 5?

bruh yes now its working — Debugging process started —
Godot Engine v3.5.3.stable.official.6c814135b - https://godotengine.org
OpenGL ES 3.0 Renderer: GeForce GTX 550 Ti/PCIe/SSE2
Async. shader compilation: OFF

colided
colided
colided
— Debugging process stopped —
But if my contacts report is 5 thats mean it will colide only 5 times ?

aww sorry that wasnt that

func body_collided(body):
		speed += increase_amount 
		print("colided")
	
func _on_YarnMouse_body_entered(body):
	if body.is_in_group("Paws"):
		emit_signal("score_increment")
		print("entered")

my func body entered still not working

after changing the group name with wall thats my borders it works fine but with paws not but its correct name