Parkster00
Parkster00

Reputation: 1

Attempt to call function "is stopped" in base "null instance" on a null instance in Godot

I'm really unsure why I'd be getting this error. BulletCooldownNode is a timer object in Godot, it's a child of the script. It started this ever since I made the script global. ANy ideas?

extends CharacterBody2D
@export var Player : PackedScene

var Bullet = load("res://Scenes/Projectiles/player_bullet.tscn")
var BigBullet = load("res://Scenes/Projectiles/player_bullet(BIG).tscn")
@onready var bulletCooldownNode := $BulletCooldownNode
@onready var starting_position = global_position
.
.
.
func get_input():
    look_at(get_global_mouse_position())
    
    set_rotation_degrees(max(min(75, get_rotation_degrees()), -75))
    
    # Shoot
    if Input.is_action_pressed("Shoot") and bulletCooldownNode.is_stopped():
        shoot()

I'm expecting it to shoot the projectile with a cooldown timer

Upvotes: 0

Views: 576

Answers (1)

MeSteve95
MeSteve95

Reputation: 131

When you made the script global, that causes for it to get initialized at a different time from the rest of the nodes in the scene. Because of this, the BulletCooldownNode has not been initialized when the script above is trying to access it.

You should ask yourself whether you need the script to be global, and only have global scripts if it is absolutely necessary to reference that script from any scene in your game. There are many times when people make scripts into autoloads when they do not need to, and it causes for issues like the one you're experiencing.

It can be difficult, but changing how you approach this problem will likely help you find a solution that works without needing global variables. If you would like some suggestions of how to accomplish this feel free to let me know and I'd be happy to provide some ideas!

Upvotes: 1

Related Questions