• Godot HelpProgramming
  • Attempt to call function 'project_ray_origin' in base 'null instance' on a null instance.

Hello! I'm new to the forum. I have the following question, I have created a script named 'script.gd' and assigned it to the Camera, I want to develop a RTS game. I need to be able to move a Warrior to where the mouse points to.

The code of script.gd is as follows (taken from Godot Engine Q&A)

extends Node

# Declare member variables here. Examples:
# var a = 2
# var b = "text"


# Called when the node enters the scene tree for the first time.
func _ready():
	pass

func _input(event):
	if (event is InputEventMouseButton and event.pressed and event.button_index == 1):
		var camera = $Camera
		var mouse_pos = get_viewport().get_mouse_position()
		var from = camera.project_ray_origin(mouse_pos)
		var to = from + camera.camera_project_ray_normal(mouse_pos) * camera.distance_from_camera
		# Apply the position to whatever object you want
		get_node("Warrior").global_transform.origin = to

Sorry, I get the following error: Attempt to call function 'project_ray_origin' in base 'null instance' on a null instance.

Welcome to the forums @nestorac!

Does the node this script is attached to have a child Camera node called Camera? The error is saying that the you are trying to call a function called project_ray_origin on a null/non-existent object. This generally occurs when you are using get_node or $ (shorthand for get_node) but the node does not exist in the scene tree or has a different name.

That means your camera doesn't exist, or is named something other than "Camera", or is named "Camera" but is not a child on that node the script is on.

Thank you! I'm quite busy, I'll take a look tomorrow, it sounds right :-)

I have moved the script to the root node, it says:

Invalid call. Nonexistent function 'camera_project_ray_normal' in base 'Camera'.

So, now project_ray_origin works, but camera_project_ray_normal does not work. Thank you!

So if you hold Control and click on a function, it will bring up the documentation inside Godot. Check that the function actually exists, that it is named properly and that you are calling it on the correct object.

7 days later

Thank you @TwistedTwigleg , it works :-) I'll try to continue with the game, it's quite challenging. My next step is to be able to move the camera with the mouse and also properly move the warrior. @cybereality , it's really useful to be able to easily read the docs, thank you!

a year later