0 votes

I used random generation of value in one of my project and when i tried to use that in my timer node it worked perfectly at first, but later it crushed and said that it was an null instance, when i used just timer instance. I tried to rewrite code (the part responsible for it, I mean), delete and add the timer node and restart the godot. I didn't restart the compuer though. Can anyone help me fix it?
my code is:

 extends KinematicBody2D

var rng2 = RandomNumberGenerator.new()
var rng = RandomNumberGenerator.new()
var offset = Vector2.AXIS_Y
var direction = Vector2.AXIS_X
var velocity = Vector2.ZERO
var left = Vector2.LEFT
var right = Vector2.RIGHT
var down = Vector2.DOWN
export var health = 1
var turn = Turn
var diection = "right"
var speed_multiplayer = 0.1
var speed = 0.5
var death_effect_sound = false


onready var Scoree = $"/root/Score"
onready var winn = $"/root/WinnDetector"
onready var animplay = $SoundPlayer
onready var tamir = $AttackTimerr

signal higher_score


func _ready():
    rng.randomize()
    var my_random_number = rng.randf_range(6.0, 12.0)
    tamir.start(my_random_number)
    offset += 2
    velocity.x = 1
    health = rng2.randf_range(1.0, 2.0)


func _physics_process(delta):
    if health <= 0:
        create_death_effect()
    move_and_collide(velocity)
    if diection == "right":
        velocity.x = speed
    elif diection == "left":
        velocity.x =0 - speed




func create_death_effect():
    var Death_effect = load("res://Enemy/Death Effect Enemy.tscn")
    var df = Death_effect.instance()
    var world = get_tree().current_scene
    get_parent().add_child(df)
    df.global_position = global_position
    Scoree.score += 5
    winn.enemy_count -= 1
    death_effect_sound = true
    queue_free()





func _on_Hurtbox_area_entered(area):
    emit_signal("higher_score")
    health -= 1
    var explosion = load("res://Effect/Explosion.tscn")
    var ex = explosion.instance()
    var world = get_tree().current_scene
    get_parent().add_child(ex)
    ex.global_position = global_position
    animplay.play("Hit")

func _on_Area2D_area_entered(area):
    speed = speed + speed * speed_multiplayer
    global_position.y += 3
    if diection == "left": 
        diection = "right"
    elif diection == "right":
        diection = "left"



func _on_KillTimer_timeout():
    queue_free()


func _on_AttackTimer_timeout():
    var my_random_number = rng.randf_range(6.0, 12.0)
    tamir.start(my_random_number)
    var Bullet = load("res://Enemy/Enemy Bullet.tscn")
    var bullet = Bullet.instance()
    var world = get_tree().current_scene
    get_parent().add_child(bullet)
    bullet.global_position = global_position
    animplay.play("attack")

the error was

Attempt to call a function 'start' in base 'null instance' on a null instance
Godot version v3.4.2
in Engine by (12 points)

1 Answer

0 votes

Your start function is called by tamir, so we need to investigate why tamir is null.

onready var tamir = $AttackTimerr

Are there supposed to be two R's on timer?

by (19 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.