Null error when trying to get script from node

Godot Version

4.1

Question

I am using a raycast to interact with an object, and I’m trying to get a value from a script I have attached to said object, but my code is causing a null error (Script has been trimmed, but some of the movement variables remain)

using System;

public partial class CharaController : CharacterBody2D
{
	public bool Enabled = true;
	[Export] public float MoveSpeed = 5f;
	[Export] public AnimationPlayer animPlayer;
	[Export] public RayCast2D Interaction;

	public string Dir = "down";
	
	// Called when the node enters the scene tree for the first time.
	public override void _Ready()
	{
	}

	public override void _Process(double delta)
	{
		if (Input.IsActionJustPressed("z"))
    	{
			Node collidingObject = Interaction.GetCollider() as Node;
			DialogueObject LookForDialogue = (DialogueObject)collidingObject.GetScript();
			if(true) //When I get the null error to be fixed this will check if it is null, because there will be multiple outcomes of interacting with an object
			{
				DialogueResource dialogueResource = LookForDialogue.dialogueObject; //This is causing the null error
				GD.Print(dialogueResource.dialogue);
			}
			
		}
	}
}

I found the issue. The script I was trying to get was attached to the collisionshape, not the area2d.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.