I was following a tutorial on Youtube about making a 3d top down shooter. Whenever I try to click to shoot my game crashes and I get the error
Attempt to call function 'shoot' in base 'null instance' on a null instance.
this is the code I have. this isn't my own original code.
extends KinematicBody
onready var gun_controller = $guncontroller
var speed = 8
var velocity = Vector3()
func _process(delta):
#movement
velocity = Vector3()
if Input. is_action_pressed("ui_right"):
velocity.x -= 1
if Input. is_action_pressed("ui_left"):
velocity.x += 1
if Input. is_action_pressed("ui_up"):
velocity.z += 1
if Input. is_action_pressed("ui_down"):
velocity.z -= 1
velocity = velocity.normalized() * speed
move_and_slide(velocity)
if Input.is_action_pressed("primary_action"):
gun_controller.shoot()
its saying the error is from the last line of code above. just incase it is needed also Im putting the code for the gun controller and gun itself.
gun controller code
extends Node
export(PackedScene) var StartingWeapon
var hand :Position3D
var equipped_weapon : Spatial
func ready():
hand = getparent().findnode("hand")
if StartingWeapon:
equipweapon(StartingWeapon)
func equipweapon(weapontoequip):
if equippedweapon:
print("Deleting current weapon")
equippedweapon. queuefree()
else:
print("No weapon equipped")
equippedweapon = weapontoequip. instance()
hand.addchild(equipped_weapon)
func shoot():
if equippedweapon:
equippedweapon.shoot()
Gun Code
extends Spatial
export(PackedScene) var bullet
onready var roftimer = $Timer
var canshoot = true
export var muzzlespeed = 30
export var millisbetween_shots = 100
func ready():
roftimer.waittime = millisbetween_shots / 1000.0
func _process(delta):
pass
func shoot():
if canshoot:
var newbullet = bullet.instance()
newbullet.globaltransform = $muzzle.globaltransform
newbullet.speed = muzzlespeed
var sceneroot = gettree().getroot().getchildren()[0]
sceneroot.addchild(newbullet)
print("pew")
canshoot = false
roftimer.start()
Lastly Im posting the line to the tutorial I was using so if anyone needs an better idea of how it works
Tutorial video