Invalid get index 'global_transform' (on base: 'null instance').

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By newgodot_user

following FPS tutorials by Garbaj

full script:

extends KinematicBody

var damage = 20
var spread = 10

var speed = 20
var h_acceleration = 6
var air_acceleration = 1
var normal_acceleration = 6
var gravity = 20
var jump = 10
var full_contact = false

var mouse_sensitivity = 0.2

var direction = Vector3()
var h_velocity = Vector3()
var movement = Vector3()
var gravity_vec = Vector3()

onready var head = $Head
onready var ground_check = $GroundCheck
onready var aimcast = $Head/Camera/AimCast
onready var bullet_hole = preload("res://Assets/BulletDecal.tscn")

onready var ray_container = $Head/Camera/RayContainer
onready var muzzle = $Head/Camera/SPAS12/Muzzle

func _ready():

	randomize()
	for r in ray_container.get_children():
			r.cast_to.x = rand_range(spread, -spread)
			r.cast_to.y = rand_range(spread, -spread)
	
func _input(event):
	if event is InputEventMouseMotion:
		rotate_y(deg2rad(-event.relative.x * mouse_sensitivity))
		head.rotate_x(deg2rad(-event.relative.y * mouse_sensitivity))
		head.rotation.x = clamp(head.rotation.x, deg2rad(-89), deg2rad(35))
		
func fire_shotgun():
	if Input.is_action_pressed("fire"):
		for r in ray_container.get_children():
			r.cast_to.x = rand_range(spread, -spread)
			r.cast_to.y = rand_range(spread, -spread)
			if r.is_colliding():
				if r.get_collider().is_in_group("Enemy"):
					r.get_collider().health -= damage
					
func _process(delta):
	fire_shotgun()
	
func _physics_process(delta):
	
	direction = Vector3()
	
	if Input.is_action_just_pressed("fire"):
		if aimcast.is_colliding():
			var bullet = get_world().direct_space_state
			var collision = bullet.intersect_ray(muzzle.global_transform.origin, aimcast.get_collision_point())
			
			if collision:
				var target = collision.collider
				if target.is_in_group("Enemy"):
					print("Hit Enemy")
					target.health -= damage
			
	if ground_check.is_colliding():
		full_contact = true
	else:
		full_contact = false
	
	if not is_on_floor():
		gravity_vec += Vector3.DOWN * gravity * delta
		h_acceleration = air_acceleration
	elif is_on_floor() and full_contact:
		gravity_vec = -get_floor_normal() * gravity
		h_acceleration = normal_acceleration
	else:
		gravity_vec = -get_floor_normal()
		h_acceleration = normal_acceleration
		
	if Input.is_action_just_pressed("jump") and (is_on_floor() or ground_check.is_colliding()):
		gravity_vec = Vector3.UP * jump
	
	if Input.is_action_pressed("move_forward"):
		direction -= transform.basis.z
	elif Input.is_action_pressed("move_backwards"):
		direction += transform.basis.z
	if Input.is_action_pressed("move_left"):
		direction -= transform.basis.x
	elif Input.is_action_pressed("move_right"):
		direction += transform.basis.x
		
	direction = direction.normalized()
	h_velocity = h_velocity.linear_interpolate(direction * speed, h_acceleration * delta)
	movement.z = h_velocity.z + gravity_vec.z
	movement.x = h_velocity.x + gravity_vec.x
	movement.y = gravity_vec.y
	
	move_and_slide(movement, Vector3.UP)
:bust_in_silhouette: Reply From: bellpepper

The error means that the object you tried to reference and get the global_transform property from doesn’t exist.

It’s highly likely you wrongly pathed what you wanted to reference. It’s impossible for anyone here to answer how you wrongly referenced because we don’t have an image of your node tree.

It seems you may be in a very beginner stage of programming in general. I suggest learning more basics before jumping into the deep end than following tutorials exactly, since its harder to learn anything that way.

All right, thanks!

newgodot_user | 2020-11-10 07:21

:bust_in_silhouette: Reply From: hello64

i forgot to click answer

This sometimes happens when the node that is trying to access the other node is a child of the node its trying to access

hello there

not to call you out or anything, but you’re kind of late. like 2 years late.

but anyways: okay thanks. i stopped working on this though shortly after the first answer.

newgodot_user | 2022-08-05 12:14